Mailing List Archive

opening more than 1 file
I have a variable, which can have a value in the range from 1 to 20.

If the value is 7, I have to open 7 files.

What could be an elegant way of doing this?

Martin
opening more than 1 file [ In reply to ]
martin van nijnatten wrote:
>
> I have a variable, which can have a value in the range from 1 to 20.
>
> If the value is 7, I have to open 7 files.
>
> What could be an elegant way of doing this?

Well, how about:

files = []
for i in range(nfiles):
files.append(open("/tmp/file%03d"%i, "wb"))

then access the i-th file as files[i]?

--
Skip Montanaro | Mojam: "Uniting the World of Music"
http://www.mojam.com/
skip@mojam.com | Musi-Cal: http://www.musi-cal.com/
518-372-5583
opening more than 1 file [ In reply to ]
martin van nijnatten wrote:
>
> I have a variable, which can have a value in the range from 1 to 20.
>
> If the value is 7, I have to open 7 files.
>
> What could be an elegant way of doing this?

It's hard to say without more details -- what names do you want these
files to have, for instance? Do you want to read the files, or write to
them? The basic idea could be something like:

def openfiles(howmany):
opened_files = []
for i in range(howmany):
opened_files.append(open("prefix%s" % i, "r"))
return opened_files

This returns a list with file objects.

Why is it necessary to open so many files simultaneously anyway? Perhaps
I misunderstood. :)

Regards,

Martijn
opening more than 1 file [ In reply to ]
martin van nijnatten wrote:
>
> I have a variable, which can have a value in the range from 1 to 20.
>
> If the value is 7, I have to open 7 files.
>
> What could be an elegant way of doing this?
>
> Martin


If your definition of `elegant' includes `short', here is a one-liner to
do this. It maps a list [0,1,2,...] to a list of open files:

[.<open file 'file00', mode 'w' at 100bb2e8>,
<open file 'file01', mode 'w' at 100bb338>,
<open file 'file02', mode 'w' at 100bb388>,
...]

files = map(lambda i: open("file%02d"%i, 'w'), range(N))

Then, files[i] gives the i'th file.

--
Dr. Gary Herron <gherron@aw.sgi.com>
206-287-5616
Alias | Wavefront
1218 3rd Ave, Suite 800, Seattle WA 98101
opening more than 1 file [ In reply to ]
Thanks, this answers my question.

I want to read the files, and they have to be open simultaneously because I
want to
merge the records/lines in the files into 1 file (after I made some changes
to certain fields)

Martijn Faassen wrote:

> martin van nijnatten wrote:
> >
> > I have a variable, which can have a value in the range from 1 to 20.
> >
> > If the value is 7, I have to open 7 files.
> >
> > What could be an elegant way of doing this?
>
> It's hard to say without more details -- what names do you want these
> files to have, for instance? Do you want to read the files, or write to
> them? The basic idea could be something like:
>
> def openfiles(howmany):
> opened_files = []
> for i in range(howmany):
> opened_files.append(open("prefix%s" % i, "r"))
> return opened_files
>
> This returns a list with file objects.
>
> Why is it necessary to open so many files simultaneously anyway? Perhaps
> I misunderstood. :)
>
> Regards,
>
> Martijn
opening more than 1 file [ In reply to ]
Thanks, this answers my question.
Thanks, this answers my question.


Skip Montanaro wrote:

> martin van nijnatten wrote:
> >
> > I have a variable, which can have a value in the range from 1 to 20.
> >
> > If the value is 7, I have to open 7 files.
> >
> > What could be an elegant way of doing this?
>
> Well, how about:
>
> files = []
> for i in range(nfiles):
> files.append(open("/tmp/file%03d"%i, "wb"))
>
> then access the i-th file as files[i]?
>
> --
> Skip Montanaro | Mojam: "Uniting the World of Music"
> http://www.mojam.com/
> skip@mojam.com | Musi-Cal: http://www.musi-cal.com/
> 518-372-5583
opening more than 1 file [ In reply to ]
"Gornauth" <gornauth@dds.nl> writes:

> Gary Herron wrote in message <371CCAE0.7DE87F8A@aw.sgi.com>...
> >files = map(lambda i: open("file%02d"%i, 'w'), range(N))
>
> I'm not completely sure how that one-liner works. To be honest, I have no
> clue whatsoever.

The above fragment is equivalent to:

def open_file(i):
filename = "file%02d" % i
file = open(filename, 'w')
return file

files = map(open_file, range(N))

And the last line could be replaced by:

files = []
for i in range(N):
file = open(file(i))
files.append(file)

Basically, 'lambda' is handy when you need to "def"ine a function but
don't want to define a function, and map is handy when you need to do a
for-loop but you don't want to do a for-loop. This allows one to write
"simple" (as in "short") programs.

> Could someone more python-literate please be so kind as to give a couple of
> examples on how to use 'map' and 'lamba'?

To start with map(): map() applies a given function to every element
of a given list. In most cases, the same result could have been had
with a for-loop. It is best for simple functions, especially built-ins,
because in that case it's faster than the resulting for-loop, and for
a simple function I find map often clearer.

e.g. you have a list with integers and want to convert them all to strings:

strlist = map(str, intlist)

as opposed to:

strlist = []
for i in intlist:
strlist.append(str(i))

I find the first possibility not only shorter, but also more readable.

Lambda lets you create a simple one-shot function.
It is most useful when you need to pass a function as an argument, and
the function you want to pass is very simple.

You *can* combine map and lambda to simulate very complex for-loops.
However, I think that in that case it's usually better to write out
the for-loop.


>
> Met vriendelijke groeten,
> Hans

Groeten,

Stephan
opening more than 1 file [ In reply to ]
Gary Herron wrote in message <371CCAE0.7DE87F8A@aw.sgi.com>...

>If your definition of `elegant' includes `short', here is a one-liner to
>do this. It maps a list [0,1,2,...] to a list of open files:
>
>[.<open file 'file00', mode 'w' at 100bb2e8>,
> <open file 'file01', mode 'w' at 100bb338>,
> <open file 'file02', mode 'w' at 100bb388>,
> ...]
>
>files = map(lambda i: open("file%02d"%i, 'w'), range(N))

I'm not completely sure how that one-liner works. To be honest, I have no
clue whatsoever.

Could someone more python-literate please be so kind as to give a couple of
examples on how to use 'map' and 'lamba'?

Met vriendelijke groeten,
Hans
opening more than 1 file [ In reply to ]
Gornauth writes:
>>files = map(lambda i: open("file%02d"%i, 'w'), range(N))
>
>I'm not completely sure how that one-liner works. To be honest, I have no
>clue whatsoever.

It would be equally workable to just use a for loop:

files = []
for i in range(N):
files.append( open("file%02d"%i, 'w') )

Using map() is tricky, but lets you keep the line count down;
generally one shouldn't care too much about line count. :)
Take it step by step:

* What does range(N) do? It returns a list containing the
numbers 0,1,2, ... that are less than N. range(5) returns [0, 1, 2,
3, 4].

* What does map() do? map(F, L), where F is some function and
L is some list (actually, any sequence type) returns a new list
containing the result of F applied to each element of L.

map(F, range(N)) returns [ F(0), F(1), ... F(N-1) ].
map(string.upper, ["This", "is", "a", "test"] ) returns
['THIS', 'IS', 'A', 'TEST']

* What does lambda do? It defines a function which doesn't
have a name (it's an anonymous function), and can only contain an
expression whose value is returned. It's possible to assign the
anonymous function to a name, and it will act like any other Python
function.

>>>f = lambda x: x+1
>>> f(0)
1
>>> f(3)
4

However, to define a named function it's clearer to just use
def f(x): return x+1 . lambda is therefore usually used with map()
and its cousins filter() and reduce(), where you often need a function
which does something simple. In that case, the function is only
needed for that one map(), so people are reluctant to write:

def opener(i): return open("file%02d"%i, 'w')
files = map(opener, range(N) )

The above two lines are equivalent to the one-liner using lambda; the
only difference is that now there's an opener() function lying around
in the namespace.

--
A.M. Kuchling http://starship.python.net/crew/amk/
... but whenever optimization comes up, people get sucked into debates about
exciting but elaborate schemes not a one of which ever gets implemented;
better to get an easy 2% today than dream about 100% forever.
-- Tim Peters, 22 Mar 1998