Write a program that asks the user for a file containing a program and a name for an output file. Your program should then write the program, with line numbers to the output file. For example, if the input file is:
def main():
for i in range(10):
print("I love python")
print("Good bye!")
Then the output file would be:
1 def main():
2 for i in range(10):
3 print("I love python")
4 print("Good bye!")
I know how to create a new output file but I have difficulty in adding line numbers to each line. please help! My program is:
filename = input("Please enter a file name: ")
filename2 = input("Please enter a file name to save the output: ")
openfile = open(filename, "r")
readfile = openfile.readlines()
out_file = open(filename2, "w")
save = out_file.write(FileWithLines)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…