Open a List of Files

Fredrik Lundh fredrik at pythonware.com
Wed Jan 9 11:02:52 CET 2008


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.

examples:

 >>> def foo():
...     locals()["foo"] = 1
...     print foo
...
 >>> foo()
<function foo at 0x00BE1FB0>

 >>> def foo():
...     foo = 1
...     locals()["foo"] = 2
...     print foo
...
 >>> foo()
1

</F>




More information about the Python-list mailing list