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?

3 of 4 people (75%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

Opening more than one file

Aug 9th, 2008 02:20
Sek Tea, Nathan Wallace, unknown unknown, Hans Nowak, Snippet 92, Skip Montanaro


"""
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?
Well, how about:
"""
nfiles = 3  # your number of files here [PSST]
files = []
for i in range(nfiles):
    files.append(open("/tmp/file%03d"%i, "wb"))
"""
then access the i-th file as files[i]?
"""
http://regalos-de-navidad.blogspot.com/
http://regalosdesanvalentin.blogspot.com/
http://ideas-para-regalar.blogspot.com/