astroluv
astroluv

Reputation: 793

How to check the number of empty text files in a folder in python?

I've a bunch of test files(around 2k) which i got after running a code and some of them are empty. So i want to print the number and name of those empty text files. And all of those files have same extensions. I was trying this but didn't get the desired result.I don't know where i'm making the mistake.

for filename in os.listdir("/home/Desktop/2d_spectra"):
   if filename.endswith(".ares"):
      os.stat(filename).st_size == 0
      print filename
   else:
      None

How can i do that?

Upvotes: 1

Views: 22

Answers (1)

jh314
jh314

Reputation: 27792

You probably meant to add os.stat(filename).st_size == 0 to the if:

for filename in os.listdir("/home/Desktop/2d_spectra"):
   if filename.endswith(".ares") and os.stat(filename).st_size == 0:
      print filename
   else:
      None

Upvotes: 1

Related Questions