Open a List of Files

Martin Marcher martin at marcher.name
Tue Jan 8 07:04:13 EST 2008


BJ Swope wrote:

> On Jan 8, 2008 6:03 AM, Fredrik Lundh <fredrik 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')

files = dict()
l = ['messages', 'recipients', 'viruses']
for f in l:
        files[f] = open(s.path.join(Host_Path, outfile), "w")

files["messages].write("a string")

hth
martin

PS: Code is untested

-- 
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.




More information about the Python-list mailing list