Screwing Up looping in Generator

Rhodri James rhodri at kynesim.co.uk
Wed Jan 4 08:47:14 EST 2017


On 04/01/17 02:10, Deborah Swanson wrote:
> Sayth Renshaw wrote, on January 03, 2017 5:36 PM
>>
>> So can I call the generator twice and receive the same file
>> twice in 2 for loops?
>>
>> Once to get the files name and the second to process?
>>
>>  for file in rootobs:
>>         base = os.path.basename(file.name)
>>         write_to = os.path.join("output",
>> os.path.splitext(base)[0] + ".csv")
>>         with open(write_to, 'w', newline='') as csvf:
>>             for file in rootobs:
>>               # create and write csv
>>
>> Cheers
>>
>> Sayth
>
> I don't see why not, if you let the first one run to completion and then
> do it again a second time. Assuming your generator doesn't somehow
> delete or modify the file specifications as it yields them. It would be
> helpful to see the code for rootobs, if you have it.

Ahem.  If Sayth is using the correct terminology and rootobs actually is 
a generator (not, say, a list or tuple), then no it won't work.  Once a 
generator is exhausted, it's exhausted.  Besides, the nested for-loops 
over the same iterable is a dead giveaway that something is wrong.


-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list