5

I have-

data=pd.read_csv('data.csv')
if data.empty==False:
    do something

In my code the dataframe data generated sometimes is empty depending on some conditions. Now when the csv file is empty its throwing error-

EmptyDataError: No columns to parse from file

what should I do to avoid this error?

2
  • can you define empty, does it have headers? Commented Feb 7, 2020 at 12:56
  • @Datanovice no it doesnt Commented Feb 7, 2020 at 12:57

1 Answer 1

5

You can use try-except construction:

import pandas as pd

try:
    data = pd.read_csv('data.csv')
except pd.errors.EmptyDataError:
    print('Empty csv file!')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.