Getting file size in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Getting a file size in Python is straightforward, but the best API depends on whether you are already using pathlib or older os.path functions. In both cases, Python asks the operating system for file metadata and returns the size in bytes.
The Most Common Approaches
If you are already using pathlib, Path.stat().st_size is the cleanest option:
If you are working with older path strings, os.path.getsize is equally valid:
Both return the size in bytes and rely on the same underlying file metadata.
What stat() Gives You
stat() returns more than file size. It also provides timestamps, permission bits, and other metadata:
If you need several pieces of metadata, calling stat() once and reusing the result is cleaner than several separate filesystem lookups.
Handle Missing Files Explicitly
If the path does not exist, metadata lookup raises an exception:
That is better than assuming the file is present, especially when the path comes from user input or another system.
Convert Bytes to Human-Readable Units
Users often want to see KB, MB, or GB instead of raw bytes. A helper function can do the conversion:
This is display logic only. Keep the raw byte count for comparisons and limits.
Files and Directories Are Different Problems
st_size on a directory does not tell you the total size of all files inside that directory tree. It reports metadata about the directory entry itself.
If you want the size of everything inside a directory, you must walk it:
That distinction matters in cleanup tools, storage analyzers, and backup scripts.
pathlib vs os.path
Both are fine, but pathlib tends to produce more readable code when you are already doing other path operations:
If you are maintaining older code that already uses os.path, there is no need to rewrite everything only for file-size lookup.
Symbolic Links and Special Cases
For ordinary files, the common APIs work exactly as expected. But symbolic links and special files can introduce ambiguity about whether you want the link's own metadata or the target's metadata. That matters more in low-level tooling than in everyday scripts, but it is worth remembering when results look odd.
Common Pitfalls
- Forgetting that file size is returned in bytes.
- Calling
st_sizeon a directory and expecting the total size of everything under it. - Ignoring
FileNotFoundErrorwhen the file may not exist. - Repeating
stat()calls unnecessarily when one lookup would be enough. - Confusing display formatting with the actual stored numeric size.
Summary
- Use
Path.stat().st_sizeoros.path.getsizeto read file size in bytes. - '
pathlibis often the most readable choice in modern Python code.' - Handle missing files explicitly instead of assuming the path exists.
- Convert bytes to human-readable units only for display.
- Directory tree size is a separate problem that requires walking the files recursively.

