Open In App

Check If a Text File Empty in Python

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Before performing any operations on your required file, you may need to check whether a file is empty or has any data inside it. An empty file is one that contains no data and has a size of zero bytes. In this article, we will look at how to check whether a text file is empty using Python.

Check if a Text File is Empty in Python

Below are some ways by which we check whether a text file is empty in Python:

Reading the first character of a text file (Naive Approach)

In this approach, we first open the file in read mode and try reading only the first character of the specified file using the read() method. This method takes an argument that indicates the number of characters to be read from the file. We pass `1` to read just the first character from the file opened. If the read() method cannot read the first character, it will return None. If the returned value is None, it means the file is empty.

Python3




file_path = '/Users/girish/Desktop/GFG Internship/input.txt'
 
# open the file in read mode
with open(file_path, 'r') as file_obj:
    # read first character
    first_char = file_obj.read(1)
 
    if not first_char:
        print("File is empty")
    else:
        print("File is NOT empty")


Output:

File is empty
ospathgetsize()

Check whether the file is empty by reading the first character of the file

Using os.path.getsize() method

The os.path.getsize() method in Python is used to determine the size of a given path. It returns the size of the file at the specified path in bytes. If the file does not exist, the procedure returns a FileNotFoundError.

Python3




# import required libraries
import os
 
# file_path to check whether it is empty
file_path = "/Users/girish/Desktop/GFG Internship/input.txt"
 
try:
    # get the size of file
    file_size = os.path.getsize(file_path)
 
    # if file size is 0, it is empty
    if (file_size == 0):
        print("File is empty")
    else:
        print("File is NOT empty")
 
# if file does not exist, then exception occurs
except FileNotFoundError as e:
    print("File NOT found")


Output:

File is empty
ospathgetsize()

Check whether the file is empty using the os.path.getsize()

Using os.stat() method

The os.stat() method in Python executes stat() system call on the provided path. It is used to obtain the status of a specific path. It returns various status results, out of which we will be using `st_size` to get the size of the file. If the file does not exist, the procedure returns a FileNotFoundError.

Python3




# import required libraries
import os
 
# file_path to check whether it is empty
file_path = "/Users/girish/Desktop/GFG Internship/input1.txt"
 
try:
    # get the size of file
    file_size = os.stat(file_path).st_size
 
    # if file size is 0, it is empty
    if (file_size == 0):
        print("File is empty")
    else:
        print("File is NOT empty")
 
# if file does not exist, then exception occurs
except FileNotFoundError as e:
    print("File NOT found")


Output:

File is NOT empty
osstat()

Check whether the file is empty using os.stat()

Using file.seek() and file.tell() method together

In this approach, we make use of file.seek() and file.tell() methods to calculate the size of file and check whether the file is empty. We open the file in read mode, and then move file pointer to the end of file using file.seek(0, os.SEEK_END). Then we obtain the file size using file.tell(). Now if this returned value is zero, it means the file is empty.

Python3




import os
 
# file_path to check whether it is empty
file_path = "/Users/girish/Desktop/GFG Internship/input1.txt"
 
# open the file in read mode
with open(file_path, 'r') as file_obj:
    # move the file pointer from 0th position to end position
    file_obj.seek(0, os.SEEK_END)
 
    # return the current position of file pointer
    file_size = file_obj.tell()
 
    # if file size is 0, it is empty
    if (file_size == 0):
        print("File is empty")
    else:
        print("File is NOT empty")


Output:

File is NOT empty
ospathgetsize()

Check whether the file is empty using file.seek() and file.tell()



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads