# Program to print file contents with line number prepended to each line # # Matt Bishop, MHI 289I, Fall 2023 # def main(): # get file name try: fname = input("Please enter the name of the file to print: ") except EOFError: return except: print("Could not read what you typed") return # # print file contents line by line, adding line numbers as we go # try: infile = open(fname, 'r') except IOError: print("Error opening file", fname, "for reading") return except: print("Unknown error") return # initialize line counter lineno = 1 # for each line in the file ... for line in infile: # print the line number, then the line # note the newline is in line already print("%6d." % lineno, line, end='') lineno = lineno + 1 # all done! infile.close() main()