faqts : Computers : Programming : Languages : Python : Snippets : Stream Manipulation : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes

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. :)
"""