The Wayback Machine - https://web.archive.org/web/20240604120840/https://www.geeksforgeeks.org/how-to-check-if-a-csv-file-is-empty-in-pandas/
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.



Similar Reads

How to create multiple CSV files from existing CSV file using Pandas ?
In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting in
3 min read
How to read a CSV file to a Dataframe with custom delimiter in Pandas?
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. pandas package is one of them and makes importing and analyzing data so much easier.Here, we will discuss how to load a csv file into a Dataframe. It is done using a pandas.read_csv() method. We have to import pandas library to use th
3 min read
How to skip rows while reading csv file using Pandas?
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. Pandas package is one of them and makes importing and analyzing data so much easier. Here, we will discuss how to skip rows while reading csv file. We will use read_csv() method of Pandas library for this task. Syntax: pd.read_csv(fil
3 min read
How to export Pandas DataFrame to a CSV file?
Let us see how to export a Pandas DataFrame to a CSV file. We will be using the to_csv() function to save a DataFrame as a CSV file. DataFrame.to_csv() Syntax : to_csv(parameters) Parameters : path_or_buf : File path or object, if None is provided the result is returned as a string. sep : String of length 1. Field delimiter for the output file. na_
3 min read
Pandas - DataFrame to CSV file using tab separator
Let's see how to convert a DataFrame to a CSV file using the tab separator. We will be using the to_csv() method to save a DataFrame as a csv file. To save the DataFrame with tab separators, we have to pass "\t" as the sep parameter in the to_csv() method. Approach : Import the Pandas and Numpy modules.Create a DataFrame using the DataFrame() metho
1 min read
Export Pandas dataframe to a CSV file
Suppose you are working on a Data Science project and you tackle one of the most important tasks, i.e, Data Cleaning. After data cleaning, you don't want to lose your cleaned data frame, so you want to save your cleaned data frame as a CSV. Let us see how to export a Pandas DataFrame to a CSV file. Pandas enable us to do so with its inbuilt to_csv(
2 min read
Convert Text File to CSV using Python Pandas
Let's see how to Convert Text File to CSV using Python Pandas. Python will read data from a text file and will create a dataframe with rows equal to number of lines present in the text file and columns equal to the number of fields present in a single line. See below example for better understanding. [caption width="800"]Original Text File[/caption
2 min read
How to read csv file with Pandas without header?
Prerequisites: Pandas A header of the CSV file is an array of values assigned to each of the columns. It acts as a row header for the data. This article discusses how we can read a csv file without header using pandas. To do this header attribute should be set to None while reading the file. Syntax: read_csv("file name", header=None) ApproachImport
1 min read
Reading specific columns of a CSV file using Pandas
CSV files are widely utilized for storing tabular data in file systems, and there are instances where these files contain extraneous columns that are irrelevant to our analysis. This article will explore techniques for selectively reading specific columns from a CSV file using Python. Let us see how to read specific columns of a CSV file using Pand
3 min read
How to Append Pandas DataFrame to Existing CSV File?
In this discussion, we'll explore the process of appending a Pandas DataFrame to an existing CSV file using Python. Add Pandas DataFrame to an Existing CSV File. To achieve this, we can utilize the to_csv() function in Pandas with the 'a' parameter to write the DataFrame to the CSV file in append mode. Pandas DataFrame to_csv() Syntax Syntax : df.t
3 min read