我已经看到过类似的问题,但是我的情况略有不同,因为我不知道缩进哪里,因为我使用的是“ for”循环而不是“ with”。
f = open("Roll_List", "r+")
for myline in f:
print (myline)
if CurrentUser in myline:
x = myline.split()
print (x[1])
s = str(int(x[1]) + z)
f.write(CurrentUser + " " + s)
f.close()
答案 0 :(得分:2)
尝试在for循环外执行f.close
f = open("Roll_List", "r+")
for myline in f:
print (myline)
if CurrentUser in myline:
x = myline.split()
print (x[1])
s = str(int(x[1]) + z)
f.write(CurrentUser + " " + s)
f.close()
答案 1 :(得分:2)
在完成处理后,使用with
语句自动关闭文件。
with open("Roll_List", "r+") as f:
for myline in f:
print(myline)
if CurrentUser in myline:
x = myline.split()
print(x[1])
s = str(int(x[1]) + z)
f.write(CurrentUser + " " + s)