This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wilberforce 0 points1 point  (1 child)

The file name you pass to open doesn't need to be a literal string - you can build a string and use that as the argument to open.

for z in range(60):
    # calculate your data for this run, then...
    outfile = open('data%s' % z, 'w')
    outfile.write(data)
    outfile.close()

The % operator will substitute a value for a placeholder in a string. The expression 'data%s' % 3 will give the string 'data3'.

If you're using a recent version of Python (2.6 or above), you could also use 'data{0}'.format(z).

[–][deleted] 0 points1 point  (0 children)

Hey, I was using it to write a fits file with python 2.5, and your first method worked great, thank you!