[Tutor] Reading a file

David Hutto smokefloat at gmail.com
Fri Jul 2 15:16:02 CEST 2010


In the code below, I'm trying to read from a file, and print out the lines.
I know I've seen the exact function I'm looking for here, but it seems
like the below 'should' work.

The second function(rdfil) works, if just the line number is placed
there, and th first(increment) works for printing a new number in
range for the rdfil to operate while going through the lines in the
file.
Any other options other than using linecache.getline(I'm sure there
are, just haven't jogged the right memory cell loose yet)?

def increment():
	for number in range(1, 500):
		print number ++1

def rdfil():
	outputfile = raw_input('Input file name: ')
	for line in outputfile:
		if line:
			readcont = linecache.getline(outputfile, increment) '''increment is
originally supposed to be an int for the line number being read'''
			print readcont

Thanks,
David


More information about the Tutor mailing list