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
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
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.
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.
import os
os.stat("file").st_size == 0