Saving output files sequentially

I have Python getting a long text file and breaking off 50 line chunks of it.

I would like to save those to text files with a progressive number in the name (like 001.txt, 002.txt, 003.txt ect…), but I am a little un-clear on this part.

I need to format an integer and make a string wiith leading zeros of of a fixed size (in my case 7, 0’s) .

	# Copy first 50 lines to output file
	if( evt == 5 ):
		counterStr = right$( "000000" + resr( counter ), 7 )
		outputFile = outputPrefix + resr( counter ) + ".txt"
		f=open( outputFile,'w')
		f2 = open( inputFile, 'r')
		for i in range(1,50):
			templine = f2.readline()
			f.write( templine )
		f.close()
		f2.close()

How do I to manipulate strings, like BASIC does for example with Left$(), Right$(), Mid$(),& Len() functions…

maybe that can help you


for i in range(10):
	print "%(i)07d" % vars()

that’s called formatted strings

the syntax I used here (there are other syntax possible) is “%” followed by the variable name in paranthesis, the symbol you want to use for padding and how many times, followed by “d” (for decimal). “% vars()” at the end tells Python where to find the variable.

I hope that’s clear.

Martin

or:


myfile="myfile"
for i in range(11):  
            print "%s%07d" % (myfile,i)

Same results.

Theeth & JMS:

Yeah! :smiley:

Thanks!