【发布时间】:2015-03-25 22:50:36
【问题描述】:
编写一个程序,要求用户输入一个包含程序和输出文件名称的文件。然后,您的程序应该编写程序,并将行号写入输出文件。例如,如果输入文件是:
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)
【问题讨论】:
-
换行调整是这个问题的一部分还是偶然的? (指
print("Good bye!")的移位)
标签: python text formatting