Entry
Opening more than 1 file
Jul 5th, 2000 10:00
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 162, Martijn Faassen
"""
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?
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. :)
"""