Open In App

How to check if a csv file is empty in pandas

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Reading CSV (Comma-Separated Values) files is a common step in working with data, but what if the CSV file is empty? Python script errors and unusual behavior can result from trying to read an empty file. In this article, we’ll look at methods for determining whether a CSV file is empty before attempting to read it. This will help you deal with these scenarios properly.

How to check if csv is empty

When working with csv files in Python, it is necessary to read the csv file correctly. If we attempt to read an empty csv file it occurs error. An empty CSV file is one that doesn’t contain any rows of data, only the header row (if present). Ensuring that a CSV file is not empty before reading or processing it to avoid errors. To check for an empty CSV file, we can use some methods, such as os library, and try-except block.

By using ’empty’ Attribute

Pandas is a widely for Python library for data manipulation. It is used for reading CSV files. Pandas Dataframe has an ’empty’ attribute that returns ‘True’ if the dataframe is empty and ‘False’ if it contains data.

Python3




import pandas as pd
 
# Specify the path to CSV file
csv_file_path = '/content/data_2.csv'
 
df = pd.read_csv(csv_file_path) # read csv file
 
if df.empty:    # check if file is empty
    print('File is empty')
else:
    # proceed with reading the file
    print(df.head())


Output:

File is empty

In above example, we first read the csv file using the read_csv function. Then, we check if the file is empty using the empty attribute. If it is empty, then we print a message file is empty and not perform operation. Otherwise, we proceed with reading the file and print the top few lines of the csv file.

By using TRY-EXCEPT Block

This method handle empty CSV files by using a try-except block. This is allows you to catch exceptions that may occur during the reading process. Here is an example:

Python3




import pandas as pd
 
csv_file_path = '/content/data_2.csv'
 
try:
    df = pd.read_csv(csv_file_path)  #read the CSV file
 
    if df.empty:  # Check if the DataFrame is empty
        print('CSV file is empty')
    else:
        # Proceed with data processing
        print(df.head())
except pd.errors.EmptyDataError:
    print('CSV file is empty')
except FileNotFoundError:
    print('CSV file not found')


Output:

CSV file is empty

We are read the CSV file within the try block using pd.read_csv. If CSV file is empty, then pd.errors.EmptyDataError exception will be raised, and we catch it in the first except block. If the CSV file does not exist (FileNotFoundError), it will be caught in the second except block.

Inside each except block, we print a message to inform the user of the specific issue with the file.

Check for HEADER Only

Sometimes, CSV files may contain only in header rows, which is not empty but may not contain meaningful data. To address this, count the number of lines in the file and check if there’s only one line (the header) then it is empty. Here’s an implementation:

Python3




with open("your_file.csv", "r") as file:
    line_count = sum(1 for line in file)
 
if line_count <= 1:
    print("The CSV file contains only the header.")
else:
    df = pd.read_csv("your_file.csv")
    # Process the data.


Output:

The CSV file contains only the header.

By using OS

It is another approch to check if a csv file is empty or not by using the OS library

Python3




import os
 
file_size = os.path.getsize('/content/data_2.csv'# Find the size of csv file
 
if file_size == 0:     # if size is empty
    print('File is Empty')
else:
    # proceed with reading the file
    with open('/content/data_2.csv', 'r') as read:  
        lines = read.readlines()
        print(lines)


Output:

['Name,City,Address,Pincode,Mobile no\n']

In above example, we use the getsize function from the os library to find the size of the csv file. If the size if csv file is 0,then we print a message file is empty and not perform any operation. Otherwise, we proceed with read the file using the readlines method.

Conclusion

We check if a csv file is empty or not before attempting to read it in Python. By using above approch we are not read_csv if csv is empty. You can ensure that your code gracefully handles empty files, preventing unexpected errors and optimizing your data workflow.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads