> 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,
> "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
> "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
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
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
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.
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
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
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 (
> 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.
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
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
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
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
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
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
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
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
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
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
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
21 matches
Mail list logo