我想写一个程序,给出一些整数值。我有一个文件,第一行有一个值。如何更改line的值(例如改为12)。这是我的密码,但是 这得到一个值,我想去第2行,把m加到第2行的那个数字上,但它不起作用。在
t=open('pash.txt', 'r')
g=[]
for i in range(3):
g.append(t.readline())
t.close()
g[o-1]=(int(g[o-1]))+m # o is the number of line in file
print(g[o-1])
t=open("pash.txt","w")
for i in range(3):
t.write(str(g[i]))
t.write('\n')
t.close()
您可以
open
,使用readlines
逐行读取文件,修改内容并重新write
文件:相关问题 更多 >
编程相关推荐