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
smci
  • 32,567
  • 20
  • 113
  • 146
amisha
  • 49
  • 2
  • 8

3 Answers3

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.

Iakovos Belonias
  • 1,217
  • 9
  • 25
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.

KeremV
  • 36
  • 3
0
import os
os.stat("file").st_size == 0
Anonymous
  • 659
  • 6
  • 16
  • 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! – d_kennetz Apr 05 '19 at 16:50