Python: Three ways to check if a file is empty

Three ways to check if a file is empty in Python.

This article is about to check if a file is empty in Python. We will use three different ways to check if a file is empty or not.

The three ways are

  1. Using os.stat() method.
  2. Using  os.path.getsize() method.
  3. Reading its first character.

Method-1 : Use os.stat() to check if a file is empty :

In python, there is a stat() function which can be used to get the statistics about a file.

os.stat(path, *, dir_fd=None, follow_symlinks=True)

This function

  • It accepts file path (string type) as an argument.
  • It returns an object of the structure stat containing various attributes. (Like st.size to know size of the file)
#Program :

#importing the os module
import os

#file path as string type
path_of_file = 'sample.txt'

# checking if size of file is 0
if os.stat(path_of_file).st_size == 0:
    print('File is empty')
else:
    print('File is not empty')
Output :
File is empty

As our file is empty, so it returned ‘File is empty’.

But we have to be more careful while using it, because if the file does not exist at the respective path then FileNotFoundError.

FileNotFoundError: [WinError 2] The system cannot find the file specified: FILE_NAME

So by keeping that in mind first we will check the file exists or not. We will create a separate function for that. After checking if we will find the file exists then we will check the file is empty or not.

So, let’s see the implementation of it.

#Program :

import os

#Checking the file exists or not
def check_file_empty(path_of_file):
    #Checking if file exist and it is empty
    return os.path.exists(path_of_file) and os.stat(path_of_file).st_size == 0

path_of_file = 'sample.txt'

# checking if file exist and it is empty
EmptyOrNot = check_file_empty(path_of_file)
if EmptyOrNot:
    print('File is empty')
else:
    print('File is not empty')
Output :
File is empty

Method-2 : Check if file is empty using os.path.getsize() in Python :

In Python, os module there is an another function, by using which we can check if a file is empty or not.

os.path.getsize(path)

This function takes file path as an argument and returns the size in bytes. If the file does not exists it gives FileNotFoundError.

#program :

import os

#file path
path_of_file = 'sample.txt'

# checking if size of file is 0
if os.path.getsize(path_of_file) == 0:
    print('File is empty')
else:
    print('File is not empty')
Output : 
File is empty

Method-3 : Check if the file is empty by reading its first character in Python :

We can also check a file is empty or not by reading first character of the file. So, for that it opens the file in read only mode. Then it tries to read first character of the string. If it finds first character in the file then file exists and if it does not find the first character then the file is empty.

#Program :

def file_empty(file_name):
    # open file in read mode
    # Reading first character to check file is empty or not
    with open(file_name, 'r') as read_obj:
        # reading the first character
        one_char = read_obj.read(1)
        # if first character not fpound then file is empty
        if not one_char:
           return True
    return False

path_of_file = 'mysample.txt'
# check if file is empty
EmptyOrNot = file_empty(path_of_file)
print(EmptyOrNot)
Output : 
File is empty

Leave a Comment