To check if a file is empty in Python, the easiest way is check if a file has size 0 with the os.path module getsize() function.

import os

if os.path.getsize("C:/Users/TheProgrammingExpert/example.txt") == 0: 
    print("file is empty")
else:
    print("file is not empty")

#Output:
file is empty

You can also use the os module stat() function to get the size of a file in Python and check if the file size is equal to 0.

import os

if os.stat("C:/Users/TheProgrammingExpert/example.txt").st_size == 0: 
    print("file is empty")
else:
    print("file is not empty")

#Output:
file is empty

Finally, if you are using the pathlib module and Path, you can get the size of a file with the Path.stat() function and check if the file size is equal to 0.

from pathlib import Path

if Path("C:/Users/TheProgrammingExpert/example.txt").stat().st_size == 0: 
    print("file is empty")
else:
    print("file is not empty")


#Output:
file is empty

When working with files in Python, the ability to find the statistics of a file is important.

One such piece of information which is valuable is if a file is empty or not.

We can check if a file is empty by first getting the size of a file, and then checking if the file size is equal to 0.

In Python, there are a few ways you can get the size of a file. The easiest way is with the os module, but you can also use the pathlib module.

Using os Module to Check if File is Empty in Python

The Python os module has many great functions which help us interact with the operating system of our computer.

To get the size of a file in Python, you can use the os.path module getsize() function. getsize() returns the size of the file in bytes.

After using getsize(), you can then check if the size is equal to 0.

Below is a simple example showing how you can check if a file is empty using Python.

import os

if os.path.getsize("C:/Users/TheProgrammingExpert/example.txt") == 0: 
    print("file is empty")
else:
    print("file is not empty")

#Output:
file is empty

You can also use the os module stat() function to get the size of a file in Python.

The stat() function returns various statistics about a given file. The file size is stored in the ‘st_size’ attribute.

import os

print(os.stat("C:/Users/TheProgrammingExpert/example.txt"))

if os.stat("C:/Users/TheProgrammingExpert/example.txt").st_size == 0: 
    print("file is empty")
else:
    print("file is not empty")

#Output:
os.stat_result(st_mode=33206, st_ino=562949953632850, st_dev=2117907462, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1652100546, st_mtime=1652018258, st_ctime=1644459271)
file is empty

Using pathlib Module to Check if File is Empty in Python

You can also use the pathlib module to get file size in your Python code.

With the Python pathlib module, we can perform many operations to access files and directories in our environments.

Using the pathlib module and Path, you can get the size of a file with the Path.stat() function. Then, in the same way as above, you can check if the file size is 0.

The Path.stat() function is similar to the os.stat() function.

Below is an example of how you can use the pathlib module to get a file’s size in bytes in Python and check if the file is empty.

from pathlib import Path

if Path("C:/Users/TheProgrammingExpert/example.txt").stat().st_size == 0: 
    print("file is empty")
else:
    print("file is not empty")


#Output:
file is empty

Hopefully this article has been useful for you to learn how to check if a file is empty using Python.

Categorized in:

Python,

Last Update: March 12, 2024