Open a List of Files

John Machin sjmachin at lexicon.net
Tue Jan 8 06:31:05 EST 2008


On Jan 8, 10:03 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> BJ Swope wrote:
> > given a list such as
>
> > ['messages', 'recipients', 'viruses']
>
> > how would I iterate over the list and use the values as variables and
> > open the variable names a files?
>
> > I tried
>
> > for outfile in ['messages', 'recipients', 'viruses']:
> >     filename = os.path.join(Host_Path, outfile)
> >     outfile = open(filename, 'w')
>
> > But it's not working.
>
> the code looks ok.  please define "not working".
>

To me, it looks bad. He's rebinding "outfile" inside the loop, which
is not good style, makes the reader slow down, back up, read the code
again ... however this doesn't stop this small code fragment from
opening the 3 files for write access -- assuming an import, and
suitable contents for "Host_Path".



More information about the Python-list mailing list