How to Check if a File is Empty in Python
Determining if a file is empty is a common task in file handling.
This guide explores several methods to check if a file is empty in Python, covering techniques using os.stat()
, os.path.getsize()
, and pathlib.Path()
, as well as error handling for non-existent files.
Checking File Emptiness with os.stat()
The os.stat()
method provides file status information including size. To check if a file is empty, check if the st_size
attribute of the returned object is equal to 0:
import os
if os.stat('example.txt').st_size == 0:
print('The file is empty')
else:
print('The file is not empty')
- The
os.stat()
function returns astat_result
object which has ast_size
attribute representing the size in bytes.
You can use either a relative or absolute path to the file, as in the following example:
import os
if os.stat(
'/home/tomnolan/Desktop/python_example/example.txt'
).st_size == 0:
print('The file is empty')
else:
print('The file is not empty')
1. Checking if a File is Not Empty
Use the not equals (!=
) operator to check if a file is not empty:
import os
if os.stat('example.txt').st_size != 0:
print('The file is NOT empty')
2. Handling FileNotFoundError
The os.stat()
method raises a FileNotFoundError
if the file does not exist. Use a try
/except
block to handle this:
import os
try:
if os.stat('not-found.txt').st_size == 0:
print('The file is empty')
except FileNotFoundError:
print('The specified file does NOT exist')
Checking File Emptiness with os.path.getsize()
The os.path.getsize()
method directly returns a file's size in bytes. If the size is 0, then the file is empty:
import os
# Using relative path
if os.path.getsize('example.txt') == 0:
print('The file is empty')
else:
print('The file is NOT empty')
# Using absolute path
if os.path.getsize(
'/home/tomnolan/Desktop/python_example/example.txt'
) == 0:
print('The file is empty')
else:
print('The file is NOT empty')
1. Handling OSError
os.path.getsize()
will raise an OSError
exception if the path is invalid or the file does not exist or is not accessible. Handle this with a try
/except
block:
import os
try:
if os.path.getsize('not-found.txt') == 0:
print('The file is empty')
except OSError:
print('The specified file does NOT exist')
2. Checking File Existence with os.path.exists()
Before checking the size, use os.path.exists()
to verify that the file exists:
import os
my_path = '/home/tomnolan/Desktop/python_example/example.txt'
if os.path.exists(my_path) and os.path.getsize(my_path) == 0:
print('The file is empty')
else:
print('The file does NOT exist or is not empty')
- The
os.path.exists()
method returnsTrue
if the file exists andFalse
otherwise.
Checking File Emptiness with pathlib.Path()
The pathlib
module provides a more object-oriented approach to file system interactions. You can check if a file is empty using Path.stat()
:
import pathlib
my_path = pathlib.Path(
'/home/tomnolan/Desktop/python_examples/example.txt'
)
if my_path.stat().st_size == 0:
print('The file is empty')
else:
print('The file is NOT empty')
- The
pathlib.Path()
function instantiates aPath
object that represents a path. - The
my_path.stat().st_size
will get the file’s size, like usingos.stat()
.
Handling FileNotFoundError
The Path.stat()
method raises a FileNotFoundError
if the path does not exist. Use a try
/except
block to handle this exception:
import pathlib
my_path = pathlib.Path(
'/home/tomnolan/Desktop/python_example/example.txt'
)
try:
if my_path.stat().st_size == 0:
print('The file is empty')
else:
print('The file is NOT empty')
except FileNotFoundError:
print('The file does NOT exist')