Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
How to check if the PyMongo Cursor is Empty?
Next article icon

How to check if a csv file is empty in pandas

Last Updated : 16 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
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.


Next Article
How to check if the PyMongo Cursor is Empty?

S

siddheskjyi
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • Python-pandas
  • Geeks Premier League 2023
Practice Tags :
  • python

Similar Reads

  • How To Check If Cell Is Empty In Pandas Dataframe
    An empty cell or missing value in the Pandas data frame is a cell that consists of no value, even a NaN or None. It is typically used to denote undefined or missing values in numerical arrays or DataFrames. Empty cells in a DataFrame can take several forms: NaN: Represents missing or undefined data.
    6 min read
  • How to check if a deque is empty in Python?
    In this article, we are going to know how to check if a deque is empty in Python or not. Python collection module provides various types of data structures in Python which include the deque data structure which we used in this article. This data structure can be used as a queue and stack both becaus
    3 min read
  • How to check if the PyMongo Cursor is Empty?
    MongoDB is an open source NOSQL database, and is implemented in C++. It is a document oriented database implementation that stores data in structures called Collections (group of MongoDB documents). PyMongo is a famous open source library that is used for embedded MongoDB queries. PyMongo is widely
    2 min read
  • How to read all CSV files in a folder in Pandas?
    Our task is to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV files in a particular folder using glob() method and then reading the file by using pandas.read_csv() method and then displaying the content. ApproachImport Required Librari
    2 min read
  • How to check dataframe is empty in Scala?
    In this article, we will learn how to check dataframe is empty or not in Scala. we can check if a DataFrame is empty by using the isEmpty method or by checking the count of rows. Syntax: val isEmpty = dataframe.isEmpty OR, val isEmpty = dataframe.count() == 0 Here's how you can do it: Example #1: us
    2 min read
  • How to delete a CSV file in Python?
    In this article, we are going to delete a CSV file in Python. CSV (Comma-separated values file) is the most commonly used file format to handle tabular data. The data values are separated by, (comma). The first line gives the names of the columns and after the next line the values of each column. Ap
    2 min read
  • Python - How to Check if a file or directory exists
    Sometimes it's necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file. In this tutorial, we will cover an important concept of file handling in Pyt
    5 min read
  • How to Check a Column is Empty or Null in MySQL?
    In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the
    4 min read
  • How to check if pandas DateTimeIndex dates belong to a list?
    To check if Pandas DateTimeIndex dates belong to a list, we can use the isin() method. In this we basically consider a subset of dates and check whether those dates are present in the list or not. Method 1. Using isin() to check whether the set of dates exist in the date column or notisin() is a met
    5 min read
  • How to Check if PySpark DataFrame is empty?
    In this article, we are going to check if the Pyspark DataFrame or Dataset is Empty or Not. At first, let's create a dataframe [GFGTABS] Python3 # import modules from pyspark.sql import SparkSession from pyspark.sql.types import StructType, StructField, StringType # defining schema schema = StructTy
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences