姬長信(Redy)

PYTHON-在输出中添加行号


例如,如果输入文件是:
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!")

我很难在每行添加行.请帮忙!我的计划是:

UPDATED***

filename = input("Please enter a file name: ")
count = 0

openfile = open(filename, "r")

for lines in openfile:
    linenumbers = openfile.write(str(count)+'/t'+lines)
    count += 1


print(count)