pandas how to skip empty file reading a list of csv files

Viewed 2485

I have 60 csv files:

fname[i]  i=0:59
while fname[19] (or maybe some other file) is empty

How can I read them to a list without error "pandas.errors.EmptyDataError: No columns to parse from file"?

2 Answers

Use try and catch the exception.

try:
    # read your files
except pandas.errors.EmptyDataError:
    pass

You will be having a list of files, before reading the file just try to check if the file is empty or not.

for file in file_list:
    if os.stat(file).st_size != 0:
        # df = pd.read_csv(file)
        # do your stuf

Or you can also try catch the error and ignore. (as suggested by Jon Clements )

for file in file_list:
    try:
        # do your usual stuff
    except pandas.errors.EmptyDataError:
        print "Found empty file : {file}".format(file=file)
Related