Get size of file on disk
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The size of a file "on disk" is not always the same as the number of bytes in the file. Filesystems allocate space in blocks, so the physical space consumed can be larger or smaller than the logical size you see in an editor. To answer the question correctly, you need to know whether you want the file length, the allocated disk usage, or both.
Logical Size Versus Allocated Size
Logical size is the number of bytes stored in the file content. Allocated size is how much filesystem space is reserved for that file.
They differ because filesystems allocate in units such as 4096 bytes. A file with one byte of content may still occupy one full block. Sparse files can go the other direction: they may report a large logical size while consuming much less actual disk space.
So these are different questions:
- how many bytes does the file contain
- how many bytes of storage does the filesystem allocate
Shell Commands on Unix-Like Systems
On macOS and Linux, stat and du tell you different things:
On systems where stat -c is not available, the exact flag syntax may differ, but the concept is the same:
- '
statgives file metadata including logical length' - '
dureports disk usage'
If you care about allocated disk space, du is usually closer to the answer you want.
Using Python
Python exposes both views:
st_size is the logical byte count. st_blocks * 512 is a common way to estimate allocated size on Unix-like systems because st_blocks reports the number of 512-byte units allocated. That value is filesystem- and platform-dependent, so always treat it as operating-system metadata rather than a universal constant.
Sparse Files Change the Picture
Sparse files are an important edge case. They reserve a large logical range without allocating every block physically.
Example:
ls may show a logical size of 1G, while du shows far less actual usage. That is not an error. It is exactly what sparse allocation is designed to do.
Which Answer Should You Use
The right metric depends on your task:
- backups and network transfer usually care about logical size
- storage accounting usually cares about allocated size
- quota analysis may need the filesystem's own block accounting
This is why GUI tools sometimes show both "Size" and "Size on disk." They are answering different operational questions.
Windows and Cross-Platform Notes
On Windows, APIs and command-line tools expose similar distinctions, though the field names differ. If you are writing cross-platform code, avoid assuming one Unix-specific field exists everywhere. Define clearly whether your function returns content length or consumed storage, then implement that per platform.
Ambiguity in naming is a common source of bugs. A function called get_file_size should document which definition of size it returns.
Common Pitfalls
- Assuming file length and on-disk usage are always the same.
- Using
st_sizewhen the real requirement is filesystem allocation. - Forgetting that sparse files can have huge logical sizes with small physical usage.
- Comparing results from
ls,stat, andduas if they should always match. - Writing cross-platform code without documenting which notion of size is returned.
Summary
- Logical size is the byte length of the file's content.
- Size on disk is the allocated filesystem space, often rounded to block boundaries.
- '
statandst_sizeanswer the logical-size question.' - '
duand allocation metadata answer the on-disk-usage question.' - Sparse files are the clearest example of why the two numbers can differ sharply.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.