Open a List of Files

BJ Swope bigblueswope at gmail.com
Tue Jan 8 21:03:08 EST 2008


On Jan 8, 2008 7:22 AM, Hrvoje Niksic <hniksic at xemacs.org> wrote:

> Fredrik Lundh <fredrik at pythonware.com> writes:
>
> > BJ Swope wrote:
> >
> >>     the code looks ok.  please define "not working".
> >>
> >> Yep, defining "not working" is always helpful! :)
> >>
> >> I want to have all 3 files open at the same time.  I will write to
> >> each of the files later in my script but just the last file is open
> >> for writing.
> >
> > to keep more than one file open, you need more than one variable, or a
> > variable that can hold more than one object.
> >
> > if the number of files may vary, put the file objects in a list.
>
> Or in a dict:
>
> open_files = {}
> for fn in ['messages', 'recipients', 'viruses']:
>    open_files[fn] = open(getfilename(fn), 'w')
>
> # open_files['messages'] is the open file 'messages' etc.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I decided that I was just trying to be "too smooth by 1/2" so I fell back to
...


messages = open(os.path.join(host_path,'messages.txt'), 'wb')
deliveries = open(os.path.join(host_path,'deliveries.txt'), 'wb')
actions = open(os.path.join(host_path,'actions.txt'), 'wb')
parts = open(os.path.join(host_path,'parts.txt'), 'wb')
recipients = open(os.path.join(host_path,'recipients.txt'), 'wb')
viruses = open(os.path.join(host_path,'viruses.txt'), 'wb')
esp_scores = open(os.path.join(host_path,'esp_scores.txt'), 'wb')


Thank you to everybody who answered.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080108/43eee656/attachment.html>


More information about the Python-list mailing list