opening more than 1 file

Gary Herron gherron at aw.sgi.com
Tue Apr 20 14:43:44 EDT 1999


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 at aw.sgi.com>
206-287-5616
Alias | Wavefront
1218 3rd Ave, Suite 800, Seattle WA 98101




More information about the Python-list mailing list