[Tutor] multiple file output

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Sep 9 23:22:11 EDT 2003


>      out = open('f[i].txt', 'w')
> 
> something like this where f[i] should be coming from some list f

String formatting operator to the rescue again

for n in range(425):
    out = open('file%3d.txt' % i, 'w')
    # do something
    out.close()

or you could just store the names in a list...

fileList =['file000.txt',
       'file001.txt',
       'file002.txt',
       'file003.txt',
       'file004.txt']

for name in fileList:
   out = open(name,'w')
   # do it
   out.close()

Alan G.



More information about the Tutor mailing list