How to check if a File is Empty in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. Check if a file is empty in Python
  2. Check if a file is empty using os.path.getsize()
  3. Check if a file is empty using pathlib.path()

# Check if a file is empty in Python

To check if a file is empty:

  1. Use the os.stat() method to get the status of the file.
  2. Use the st_size attribute to get the size of the file in bytes.
  3. If the size of the file is 0 bytes, then the file is empty.
main.py
import os # โœ… with relative path if os.stat('example.txt').st_size == 0: print('The file is empty') else: print('The file is not empty') # --------------------------------------------- # โœ… with absolute path if os.stat( '/home/borislav/Desktop/bobbyhadz_python/example.txt' ).st_size == 0: print('The file is empty') else: print('The file is not empty')

check if file is empty

The code for this article is available on GitHub

We used the os.stat() method to get the status of a file.

The method can be passed a relative path or an absolute path to the file.

The os.stat() method returns a stat_result object representing the status of the given file.

The st_size attribute of the object returns the size of the file in bytes.

main.py
import os if os.stat('example.txt').st_size == 0: print('The file is empty') else: print('The file is not empty')
If the size of the file is 0 bytes, then the file is empty.

# Checking if a file is NOT empty

You can use the not equals != operator if you need to check if the file is not empty.

main.py
import os if os.stat('example.txt').st_size != 0: print('The file is NOT empty')

checking if file is not empty

The code for this article is available on GitHub

The method will raise a FileNotFoundError exception if the specified file doesn't exist.

# Handling a case where the file doesn't exist

You can use a try/except statement if you need to handle the error.

main.py
import os try: if os.stat('not-found.txt').st_size == 0: print('The file is empty') except FileNotFoundError: # ๐Ÿ‘‡๏ธ this runs print('The specified file does NOT exist')

handling case where file does not exist

The specified file doesn't exist, so a FileNotFoundError exception is raised and is then handled by the except block.

# Check if a file is empty using os.path.getsize()

Alternatively, you can use the os.path.getsize() method.

If the os.path.getsize() method returns 0, then the file has a size of 0 bytes and is empty.

main.py
import os # ๐Ÿ‘‡๏ธ with relative path if os.path.getsize('example.txt') == 0: print('The file is empty') else: print('The file is NOT empty') # --------------------------------------------- # ๐Ÿ‘‡๏ธ with absolute path if os.path.getsize( '/home/borislav/Desktop/bobbyhadz_python/example.txt' ) == 0: print('The file is empty') else: print('The file is NOT empty')
The code for this article is available on GitHub

The os.path.getsize() method takes a path and returns the size of the path in bytes.

If the file has a size of 0 bytes, then it's empty.

# Handling a case where the specified file doesn't exist

The method raises an OSError exception if the specified file doesn't exist or is inaccessible.

Use a try/except statement if you need to handle the error.

main.py
import os try: if os.path.getsize('not-found.txt') == 0: print('The file is empty') except OSError: # ๐Ÿ‘‡๏ธ this runs print('The specified file does NOT exist')

handling case where specified file does not exist

The given path doesn't exist, so an OSError exception is raised and is then handled by the except block.

Alternatively, you can check if the file exists using the os.path.exists() method.

main.py
import os my_path = '/home/borislav/Desktop/bobbyhadz_python/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 code for this article is available on GitHub
The os.path.exists() method returns True if the provided path exists and False otherwise.

Alternatively, you can use the pathlib.Path class.

# Check if a file is empty using pathlib.path()

This is a four-step process:

  1. Instantiate the pathlib.Path() class to create a Path object.
  2. Use the stat() method to get a stat_result object.
  3. Use the st_size attribute to get the file's size.
  4. If the file's size is 0 bytes, then it's empty.
main.py
import pathlib my_path = pathlib.Path( '/home/borislav/Desktop/bobbyhadz_python/example.txt' ) if my_path.stat().st_size == 0: print('The file is empty') else: print('The file is NOT empty')
The code for this article is available on GitHub

check if file is empty using pathlib path

The pathlib.Path class is used to create a PosixPath or a WindowsPath object depending on your operating system.

The Path.stat() method returns a stat_result object just like the one from the os.stat() method.

The object has a st_size attribute that returns the size of the file in bytes.

# Handling a case where the file doesn't exist

If the specified path doesn't exist, a FileNotFoundError exception is raised.

You can use a try/except block if you need to handle the error.

main.py
import pathlib my_path = pathlib.Path( '/home/borislav/Desktop/bobbyhadz_python/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')
The code for this article is available on GitHub

Which approach you pick is a matter of personal preference. They all get the file's size.

If the file has a size of 0 bytes, then it's empty, otherwise, it isn't empty.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev