更新时间:2023-12-01 08:29:10
Use a with statement to close the file buffer and just concatenate strings:
with open('file.txt', 'r') as program:
data = program.readlines()
with open('file.txt', 'w') as program:
for (number, line) in enumerate(data):
program.write('%d %s' % (number + 1, line))