Question: Please answer in Python 3 ! Question: (linenumbers.py) Write a program that asks the user for the name of a file. The program should write
Please answer in Python 3!
Question:
(linenumbers.py) Write a program that asks the user for the name of a file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by ln_ (for linenumbers, so if the selected input file is myfile.txt, the output file would be called ln_myfile.txt). Be sure to use Try/Except to catch all exceptions.
########################################################################## ##*My Code*## try: file_name = input("Enter the file name: ") infile = open(file_name, "r") outfile = open("ln_" + file_name, "w") count = 0 for line in outfile: count = count + 1 print(str(count)+". "+line) infile.close() outfile.close() except IOError: print("Error, file not found!") except: print("Unexpected error:") ###################################################################### First time posting; been stuck on this one for days. A worded answer would be appreciated so that I can learn from this instead of being given a direct solution. Although both is fine too
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
