Open a List of Files

Paul Hankin paul.hankin at gmail.com
Wed Jan 9 05:29:14 EST 2008


On Jan 9, 10:02 am, Fredrik Lundh <fred... at pythonware.com> wrote:
> Paul Hankin wrote:
> > This can be more cleanly written using locals()
>
> > for fn in filenames:
> >     locals()[fn] = open(os.path.join(host_path, fname + '.txt', 'wb')
>
> from the reference manual:
>
>      locals()
>
>      Update and return a dictionary representing the current
>      local symbol table.
>
>      Warning: The contents of this dictionary should not be
>      modified; changes may not affect the values of local
>      variables used by the interpreter.

Thanks Fredrik! I learnt something today.

I wonder if there's a reason why it doesn't raise an exception when
you try to write to it? That would seem better to me than having it
sometimes update variables and sometimes not.

--
Paul Hankin



More information about the Python-list mailing list