How to check whether a file is empty or not
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Checking whether a file is empty is a common operation in scripting, data processing, and application development. A file is considered empty if its size is 0 bytes. The approach varies by language and platform — Bash uses the -s test flag, Python uses os.path.getsize() or os.stat(), and other languages have their own filesystem APIs. This article covers the most practical methods across Bash, Python, Java, and C#.
Bash: test -s
The -s flag in Bash returns true if the file exists and has a size greater than zero:
To check specifically for "exists and is empty" (not "does not exist"):
Bash: stat and wc
Note: stat syntax differs between macOS (-f%z) and Linux (--format=%s).
Bash: find Command
Find all empty files in a directory:
Python: os.path.getsize()
This raises OSError if the file does not exist. Wrap in a try-except or check existence first:
Python: os.stat()
os.stat() returns a stat_result object with st_size (bytes), st_mtime (modification time), and other metadata. It makes a single system call, which is slightly more efficient than calling os.path.getsize() and os.path.isfile() separately.
Python: pathlib (Modern Approach)
Reading the content works for small files but is inefficient for large files since it loads the entire file into memory just to check if it is empty.
Java
C#
Common Pitfalls
- Confusing "file does not exist" with "file is empty": Many methods raise an exception or return -1 when the file does not exist. Always check for existence first, or handle the exception.
os.path.getsize()on a missing file throwsOSError, not returns 0. - Whitespace-only files are not empty: A file containing only spaces, newlines, or tabs has a size greater than zero. If you need to check for "effectively empty" (whitespace only), read the content and call
.strip(). - Race conditions: Between checking the size and acting on the result, another process may write to or delete the file. In multi-process environments, use file locking or atomic operations.
- Permissions blocking the check: On Unix, a file may exist but be unreadable.
os.path.getsize()requires read permission on the parent directory.stat()works as long as you have execute permission on the directory. - Symbolic links:
os.path.getsize()follows symlinks and returns the target file's size.os.lstat()returns the symlink's own size (the length of the path it points to), which is never 0 even if the target is empty.
Summary
- In Bash, use
[ -s "$FILE" ]— returns true if the file is non-empty - In Python, use
os.path.getsize(path) == 0orPath(path).stat().st_size == 0 - In Java, use
file.length() == 0orFiles.size(path) == 0 - In C#, use
new FileInfo(path).Length == 0 - Always check for file existence before checking size to avoid exceptions
- A file with only whitespace is not empty — read and
.strip()if you need that check
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.