我试图在使用文件时重写文件。我正在改变一个文件中的一行,我正在读取('students.txt')到一个类似于变化的行
Raymond_Red 0000000000000 0
到
Raymond_Red 0000000000000 1
问题是,我正在收到此错误,因为我打开一个已经使用'fileinput'打开的文件
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'students.txt' -> 'students.txt.bak'
我不知道如何解决这个问题。在此先感谢您的帮助!
#Code by Byron Dalberg
import time
import fileinput
lunch = str('inout {0}.txt'.format(time.strftime("%Y-%m-%d")))
while True:
variable = input()
with open ('students.txt') as f:
for eachline in f:
name,inout,rfid = eachline.rsplit(None,2)
if variable == rfid:
print("yay")
if(inout == 0):
f.close()
linein = str('{0} {1} {2}'.format(name,inout,rfid))
lineout = str('{0} 1 {1}'.format(name, rfid))
for line in fileinput.input('students.txt', inplace=1):
line.replace(linein, lineout),
with open('inout.txt','a+') as fp:
log = str('{0} loggged out at {1}(ID: {2})\n'.format(name,time.strftime("%H:%M"),rfid))
fp.write(log)
else:
linein = str('{0} {1} {2}'.format(name,inout,rfid))
lineout = str('{0} 0 {1}'.format(name, rfid))
for line in fileinput.input('students.txt', inplace=1):
line.replace(linein, lineout),
with open('inout.txt','a+') as fp:
log = str('{0} loggged in at {1}(ID: {2})\n'.format(name,time.strftime("%H:%M"),rfid))
fp.write(log)
else:
print("nope")
提前致谢!