Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

How to Check if a File Is Empty in Python

Check a path's byte size with pathlib or os.path, and handle missing files, non-files, symlinks, and stream positions correctly.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.