Entry
Opening more than one file
Jul 5th, 2000 10:01
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 177, Gary Herron
"""
Packages: files
"""
"""
> 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>,
...]
"""
N = 5 # for example; added by PSST
files = map(lambda i: open("file%02d"%i, 'w'), range(N))
"""
Then, files[i] gives the i'th file.
"""