Open a List of Files

Terry Jones terry at jon.es
Wed Jan 9 09:32:39 EST 2008


>>>>> "BJ" == BJ Swope <bigblueswope at gmail.com> writes:

I (at least) think the code looks much nicer.

BJ> #Referring to files to write in various places...
BJ> open_files['deliveries'].write(flat_line)
BJ> open_files['deliveries'].write('\n')

If you were doing a lot with the deliveries file at some point, you could
put the file descriptor into a local variable

  deliveries = open_files['deliveries']
  deliveries.write(flat_line)
  deliveries.write('\n')

BJ> #And finally to close the opened files
BJ> for fn in open_files.keys():
BJ> open_files[fn].close()

You don't need "for fn in open_files.keys():", you can just use "for fn in
open_files:", but simpler than that is to just use the dictionary values:

for fn in open_files.values():
    fn.close()

Regards,
Terry



More information about the Python-list mailing list