-2

I have a folder, with 1000s of txt files. My code is working perfectly fine if there is data in the file.

Is there anyway to build logic in my code to check if the file is empty, and if empty to skip to the next one?

import glob

import os


path = 'path/to/file'


for filename in glob.glob(os.path.join(path, '*.txt')):
  with open(filename, 'r',encoding='ISO-8859-1') as f:
    print(filename)
    text = f.read()
    do other stuff
RustyShackleford
  • 3,462
  • 9
  • 40
  • 81

1 Answers1

1

So you want to continue to the next file if its text is empty? Easy enough:

if not text: continue
Kevin
  • 74,910
  • 12
  • 133
  • 166