opening more than 1 file

Martijn Faassen faassen at pop.vet.uu.nl
Tue Apr 20 14:10:49 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?

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




More information about the Python-list mailing list