0

I have many CSV files in a particular folder. I want to check if each file among them is empty, and if yes, print its name.

Files:

file1.csv
file2.csv
file3.csv

Expected output:

Empty file :- 
file2.csv
2
  • Duplicate of stackoverflow.com/questions/2507808/… Commented Apr 5, 2019 at 10:03
  • 1
    This is a basic Python question, not specifically pandas Commented Apr 5, 2019 at 10:23

3 Answers 3

4

This is how you do it but not with pandas.

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

An other example

import os
if os.stat(file).st_size == 0:
    do something

But if you have to do it with pandas that's the right way

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

The code in all examples is self explanatory.

Sign up to request clarification or add additional context in comments.

Comments

1

you can use the python os library. You don't need to pandas.

For example:

import os

os.path.getsize('yourfile.csv')

If file size equal to 0, it's empty.

Comments

0
import os
os.stat("file").st_size == 0

1 Comment

Hi! While this may provide an answer, it is generally discouraged on StackOverflow to leave code only answers. Please explain why this is a solution as it will help OP and future visitors. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.