f



for loop iter next if file bad

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
0
Sayth
12/21/2016 9:47:12 AM
comp.lang.python 77058 articles. 3 followers. Post Follow

3 Replies
77 Views

Similar Articles

[PageSpeed] 28

>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 =3D=3D 0:
>                next(file)
>            else:
>                yield fd

>Exact error is:

>C:\Users\Sayth\Anaconda3\envs\race\python.exe C:/Users/Sayth/PycharmProjec=
ts/bs4race/race.py data/ -e *.xml
>Traceback (most recent call last):
 > File "C:/Users/Sayth/PycharmProjects/bs4race/race.py", line 98, in <modu=
le>
>    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 retur=
n_files
>    next(file)
>TypeError: 'str' object is not an iterator

The iterator is file_list not file.
File is a str. As the exception mentions.

I suppose you want there a "continue" to iterate to the next value in the f=
ile_list



This email is confidential and may be subject to privilege. If you are not =
the intended recipient, please do not copy or disclose its content but cont=
act the sender immediately upon receipt.
0
Joaquin
12/21/2016 9:55:45 AM
On Wed, Dec 21, 2016 at 8:47 PM, Sayth Renshaw <flebber.crue@gmail.com> wrote:
> 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

"next" doesn't do what you think it does - it tries to step the thing
you give it, as an iterator. I think you might want "continue"?

ChrisA
0
Chris
12/21/2016 9:59:58 AM
Ah yes. Thanks ChrisA

http://www.tutorialspoint.com/python/python_loop_control.htm

The continue Statement:
The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

The continue statement can be used in both while and for loops.

Sayth
0
Sayth
12/21/2016 10:06:52 AM
Reply: