for loop iter next if file bad

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


Hi

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(fd.name).st_size == 0:
                next(file)
            else:
                yield fd

Exact error is:

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

Sayth



More information about the Python-list mailing list