我想从文件中取数字,给它们分配一个变量名,对变量做一些数学运算,然后写入文件中的下一行。例如,如果我有一个文件:1 2 3 4 5,那么这里是我到目前为止的缩写代码(Python 3.3)。我遇到的唯一问题是将计算结果写入下一行。预先感谢您的帮助。写入文件下一行
with open('test.txt', 'r') as f:
read_data = f.read()
a1 = (read_data[0])`a1 = (read_data[0])
print(a1) # this is just a test to see whats happening
f.close()
with open('test.txt','w') as f:
f.write(a1) #how do I get a1 to write on the next line of the file
exit()
我认为用''a''而不是''w''打开文件应该有帮助。该文件附加到文件而不是覆盖。 – elParaguayo