Re: Open a List of Files

2008-01-09 Thread Tim Chase
> 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() This can also work for standard variable names: for f in (messages, deliveries, actions,

Re: Open a List of Files

2008-01-09 Thread Terry Jones
> "BJ" == BJ Swope <[EMAIL PROTECTED]> 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 p

Re: Open a List of Files

2008-01-09 Thread Terry Jones
> "Fredrik" == Fredrik Lundh <[EMAIL PROTECTED]> writes: Fredrik> Seriously, for a limited number of files, the dictionary approach Fredrik> is mostly pointless; you end up replacing Fredrik> foo = open("foo") Fredrik> foo.write(...) Fredrik> with Fredrik> somedict["foo"] = open("foo") Fred

Re: Open a List of Files

2008-01-09 Thread Fredrik Lundh
Paul Hankin wrote: > 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. probably because it returns a standard dict

Re: Open a List of Files

2008-01-09 Thread Paul Hankin
On Jan 9, 10:02 am, Fredrik Lundh <[EMAIL PROTECTED]> 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

Re: Open a List of Files

2008-01-09 Thread Fredrik Lundh
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.

Re: Open a List of Files

2008-01-09 Thread Paul Hankin
On Jan 9, 2:41 am, Tim Chase <[EMAIL PROTECTED]> wrote: > > 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.pa

Re: Open a List of Files

2008-01-08 Thread Fredrik Lundh
Terry Jones wrote: > I think you should revisit this decision. Something like Fredrik's code is > the way to go. He used my suggestion, just for a few more files than he had in his original post. Seriously, for a limited number of files, the dictionary approach is mostly pointless; you end up

Re: Open a List of Files

2008-01-08 Thread BJ Swope
On Jan 8, 2008 9:34 PM, Terry Jones <[EMAIL PROTECTED]> wrote: > > I think you should revisit this decision. Something like Fredrik's code > is > the way to go. It has multiple advantages: > > - It's much shorter. > - It's arguably easier to add/remove to/from. > - It has less risk of error (

Re: Open a List of Files

2008-01-08 Thread Tim Chase
> 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.

Re: Open a List of Files

2008-01-08 Thread Terry Jones
Hi BJ > > Fredrik Lundh <[EMAIL PROTECTED]> writes: > > Or in a dict: > > > > open_files = {} > > for fn in ['messages', 'recipients', 'viruses']: > >open_files[fn] = open(getfilename(fn), 'w') > > I decided that I was just trying to be "too smooth by 1/2" so I fell back > to ... > > message

Re: Open a List of Files

2008-01-08 Thread BJ Swope
On Jan 8, 2008 7:22 AM, Hrvoje Niksic <[EMAIL PROTECTED]> wrote: > Fredrik Lundh <[EMAIL PROTECTED]> 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

Re: Open a List of Files

2008-01-08 Thread Hrvoje Niksic
Fredrik Lundh <[EMAIL PROTECTED]> 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 th

Re: Open a List of Files

2008-01-08 Thread Fredrik Lundh
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 th

Re: Open a List of Files

2008-01-08 Thread Neil Cerutti
On Jan 8, 2008 6:54 AM, BJ Swope <[EMAIL PROTECTED]> 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 outfi

Re: Open a List of Files

2008-01-08 Thread Martin Marcher
BJ Swope wrote: > On Jan 8, 2008 6:03 AM, Fredrik Lundh <[EMAIL PROTECTED]> 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

Re: Open a List of Files

2008-01-08 Thread Chris
On Jan 8, 1:03 pm, Fredrik Lundh <[EMAIL PROTECTED]> 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 ['me

Re: Open a List of Files

2008-01-08 Thread BJ Swope
On Jan 8, 2008 6:03 AM, Fredrik Lundh <[EMAIL PROTECTED]> 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

Re: Open a List of Files

2008-01-08 Thread John Machin
On Jan 8, 10:03 pm, Fredrik Lundh <[EMAIL PROTECTED]> 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 ['m

Re: Open a List of Files

2008-01-08 Thread Fredrik Lundh
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(Hos

Open a List of Files

2008-01-07 Thread BJ Swope
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(file