【发布时间】:2015-03-25 21:34:24
【问题描述】:
编写一个程序,询问用户一个包含程序的文件和一个输出文件的名称。然后,您的程序应该编写程序,并将行号写入输出文件。例如,如果输入文件是:
def main():
for i in range(10):
print("I love python")
print("Good bye!")
那么输出文件将是:
1 def main():
2 for i in range(10):
3 print("I love python")
4 print("Good bye!")
我知道如何创建一个新的输出文件,但我很难在每一行中添加行。请帮忙! 我的程序是:
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)
【问题讨论】:
标签: python text formatting