Screwing Up looping in Generator

Deborah Swanson python at deborahswanson.net
Tue Jan 3 01:21:58 EST 2017


Sayth Renshaw wrote, on January 03, 2017 5:55 PM
>
> On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw  wrote:
> > 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 just need it to write after each file however the
>
> with open(#file) as csvf:
>
> Keeps it all open until every file processed in an output
> file with the name of the first file in the generator.
>

In that case, I think you just need to devise your output file name scheme, and 
it looks like you want to use 'os.path.splitext(base)[0] + ".csv")'. Then  
output each file in the same for loop, before you go back for another input 
file. Just read in the file, do whatever you want to it, and then write it to 
the new file name, all in the same loop.

I don't see any need to loop through rootobs a second time.




More information about the Python-list mailing list