for loop iter next if file bad

Sayth Renshaw flebber.crue at
Wed Dec 21 04:47:12 EST 2016


I am looping a list of files and want to skip any empty files.
I get an error that str is not an iterator which I sought of understand but can't see a workaround for.

How do I make this an iterator so I can use next on the file if my test returns true.

Currently my code is.

for dir_path, subdir_list, file_list in os.walk(my_dir):
    for name_pattern in file_list:
        full_path = os.path.join(dir_path, name_pattern)

def return_files(file_list):
    Take a list of files and return file when called.

    Calling function to supply attributes
    for file in file_list:
        with open(os.path.join(dir_path, file), 'rb') as fd:
            if os.stat( == 0:
                yield fd

Exact error is:

C:\Users\Sayth\Anaconda3\envs\race\python.exe C:/Users/Sayth/PycharmProjects/bs4race/ data/ -e *.xml
Traceback (most recent call last):
  File "C:/Users/Sayth/PycharmProjects/bs4race/", line 98, in <module>
  File "C:/Users/Sayth/PycharmProjects/bs4race/", line 51, in data_attr
    for xml_data in roots:
  File "C:/Users/Sayth/PycharmProjects/bs4race/", line 32, in return_files
TypeError: 'str' object is not an iterator


More information about the Python-list mailing list