file handling
check file emptiness
programming
file operations
code tutorial

How to check whether a file is empty or not

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

bash
1FILE="/path/to/file.txt"
2
3if [ -s "$FILE" ]; then
4    echo "File is NOT empty"
5else
6    echo "File is empty (or does not exist)"
7fi

To check specifically for "exists and is empty" (not "does not exist"):

bash
1if [ -f "$FILE" ] && [ ! -s "$FILE" ]; then
2    echo "File exists and is empty"
3elif [ ! -f "$FILE" ]; then
4    echo "File does not exist"
5else
6    echo "File exists and is NOT empty"
7fi

Bash: stat and wc

bash
1# Using stat to get file size in bytes
2SIZE=$(stat -f%z "$FILE" 2>/dev/null || stat --format=%s "$FILE" 2>/dev/null)
3if [ "$SIZE" -eq 0 ]; then
4    echo "Empty"
5fi
6
7# Using wc -c (count bytes)
8if [ "$(wc -c < "$FILE")" -eq 0 ]; then
9    echo "Empty"
10fi

Note: stat syntax differs between macOS (-f%z) and Linux (--format=%s).

Bash: find Command

Find all empty files in a directory:

bash
1# List empty files in the current directory
2find . -maxdepth 1 -empty -type f
3
4# Delete empty files
5find /path/to/dir -empty -type f -delete
6
7# Find non-empty files only
8find . -type f ! -empty

Python: os.path.getsize()

python
1import os
2
3file_path = "data.txt"
4
5if os.path.getsize(file_path) == 0:
6    print("File is empty")
7else:
8    print("File is not empty")

This raises OSError if the file does not exist. Wrap in a try-except or check existence first:

python
if os.path.isfile(file_path) and os.path.getsize(file_path) == 0:
    print("File exists and is empty")

Python: os.stat()

python
1import os
2
3file_stat = os.stat("data.txt")
4if file_stat.st_size == 0:
5    print("Empty")

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)

python
1from pathlib import Path
2
3file = Path("data.txt")
4
5if file.exists() and file.stat().st_size == 0:
6    print("File is empty")
7
8# Or use the file content directly
9if file.exists() and file.read_text() == "":
10    print("File is empty")

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

java
1import java.io.File;
2import java.nio.file.Files;
3import java.nio.file.Path;
4
5// Method 1: File.length()
6File file = new File("data.txt");
7if (file.exists() && file.length() == 0) {
8    System.out.println("File is empty");
9}
10
11// Method 2: Files.size() (NIO)
12Path path = Path.of("data.txt");
13if (Files.exists(path) && Files.size(path) == 0) {
14    System.out.println("File is empty");
15}

C#

csharp
1using System.IO;
2
3string path = "data.txt";
4
5// Method 1: FileInfo
6FileInfo fi = new FileInfo(path);
7if (fi.Exists && fi.Length == 0)
8{
9    Console.WriteLine("File is empty");
10}
11
12// Method 2: Direct length check
13if (File.Exists(path) && new FileInfo(path).Length == 0)
14{
15    Console.WriteLine("File is empty");
16}

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 throws OSError, 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) == 0 or Path(path).stat().st_size == 0
  • In Java, use file.length() == 0 or Files.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

Course illustration
Course illustration

All Rights Reserved.