Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →For a file path, check whether its size in bytes is zero: Path(path).stat().st_size == 0. This reads filesystem metadata rather than loading the file contents. A missing or inaccessible path is an error, not an empty file.
Check a file path with pathlib
Path.stat() returns file metadata, including st_size, the file size in bytes. Equality with zero means the target has no bytes.
from pathlib import Path
path = Path("data.txt")
is_empty = path.stat().st_size == 0
Python documents this behavior in the pathlib documentation.
Alternative: use os.path.getsize()
If you prefer a function that returns the size directly, compare os.path.getsize() with zero:
#1 Best Overall
import os
is_empty = os.path.getsize("data.txt") == 0
getsize() accepts path-like objects as well as strings. Both it and Path.stat() require a successful filesystem lookup; Python’s os.path documentation specifies that getsize() returns the size in bytes and raises OSError if the path does not exist or is inaccessible.
| API | Interface and result | Lookup failure |
|---|---|---|
Path(path).stat().st_size |
pathlib method; read the byte size from the stat result. |
Can raise OSError. |
os.path.getsize(path) |
os.path function; returns the byte size as an integer. |
Raises OSError when the file is missing or inaccessible. |
Handle missing paths and non-files deliberately
Do not turn every failed lookup into False or treat it as an empty-file result. Choose whether your application should propagate the error, catch it, or report a separate state.
Rank #2
from pathlib import Path
path = Path("data.txt")
try:
is_empty = path.stat().st_size == 0
except OSError as exc:
# Handle a missing, inaccessible, or otherwise unqueryable path.
print(f"Cannot check file: {exc}")
If directories or other non-regular paths may be supplied, validate the type before checking size:
from pathlib import Path
path = Path("data.txt")
if not path.is_file():
is_empty = None # Choose your application's missing/non-file behavior.
else:
is_empty = path.stat().st_size == 0
Path.is_file() returns false for a missing path, a broken symlink, or another kind of path. Use stat() when you need to distinguish lookup errors rather than just receive a type check. Error suppression for pathlib’s file-type and existence methods varies by Python version: Python 3.14 suppresses all OSError exceptions in these methods, while earlier versions may behave differently. Consult the documentation for your supported Python version.
Know what a symlink check measures
By default, Path.stat() follows a symbolic link and reports the target’s size. If you mean the link entry itself, use Path.lstat() or os.stat(..., follow_symlinks=False) where supported. In that case, st_size is the byte length of the pathname stored in the link, not the size of the target’s contents. Python describes these distinctions in the pathlib and os documentation.
Why tell() does not test whether a file is empty
f.tell() == 0 means the current stream position is zero; it does not establish that the underlying file has zero bytes. A stream can be at its beginning and still contain data. Text-stream tell values can also be opaque rather than byte counts, and some streams do not support seeking or telling. For a path’s size, use stat() or getsize(). For an already-open stream, decide whether you mean remaining content and account for preserving or restoring its position, as explained in Python’s io documentation.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




