Screwing Up looping in Generator

Terry Reedy tjreedy at
Tue Jan 3 16:57:52 EST 2017

On 1/3/2017 3:53 PM, Deborah Swanson wrote:

>> I think you're expecting
>> 	for file in rootobs
>> to get the next yield for you from rootobs, but unless
>> someone corrects me, I don't think you can expect a 'for'
>> statement to do that. You need to have a 'next' statement
>> inside your for loop to get the next yield from the generator.

As I read this, out of context, it is wrong.  It it very unusual to call 
next on the current iterator (here next(rootobs)), inside a for loop.

> You probably want something like :
> for f in rootobs:
>     file = next

This is definitely wrong, as it makes 'find' an alias for the next() 

>     base = os.path.basename(

and will be an AttributeError.

If one wants to iterate through files and lines within files, which I 
believe I saw in this thread, one should have a for loop within a for loop.

Terry Jan Reedy

More information about the Python-list mailing list