# Program to print file contents with line number prepended to each line # This one writes to a file with the same base name but with ".lst" appended # Matt Bishop, Apr. 17, 2009 # for ECS 10 Spring 2009 # import string; def main(): # get input file name iname = raw_input("Please enter the name of the file to print:"); # get the listing file name pos = string.rfind(iname, "."); oname = iname[0:pos] + ".lst" # # open input, output files # infile = open(iname, 'r'); outfile = open(oname, 'w'); # # line by line, adding line numbers as we go # # initialize line counter lineno = 1; # for each line in the file ... for line in infile: # print the line number, then the line outfile.write("%6d. " % lineno,); outfile.write(line,); lineno = lineno + 1; # all done! infile.close(); outfile.close(); main();