I'm trying to do inplace editing of a file using the fileinput module, but it doesn't seem to want to work.
The code I'm using:
for line in fileinput.FileInput(complaint_db, inplace=True, backup=(complaint_db + ".backup")):
print("Started replacement")
if line == myline:
pass
else:
print(line)
The backup argument is modified because I thought it might fix the error, it didn't. The file does not exist before I run this command (I have checked a hundred times) nor does it after. I'm creating the file in my home directory so there should be no error.
Here is the full error:
builtins.PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'c:\\Users\\Cody\\Documents\\ComplaintManagement\\complaints.dbc:\\Users\\Cody\\Documents\\ComplaintManagement\\complaints.db.backup'
I guess another question is, how do I check if the original complaints.db is open somewhere in the file without knowing where it might be open. If so, can I universally close it at will from any point in the code?
I can't do a f.close because f won't be defined at this point in the code. I tried os.close(complaint_db) (complaint_db is a universal variable holding the location of the db). It won't work because it requires an int, so I'm kind of lost now.
I fixed this by using a different form of changing the database. Instead of fileinput, I changed it to the following code:
from shutil import move
def RemoveClaim(self, myline):
ocdb = open(complaint_db, 'r')
ncdb = open(complaint_db + "2", 'w')
for line in ocdb:
if line == myline:
pass
else:
ncdb.write(line)
ocdb.close()
ncdb.close()
move(complaint_db + "2", complaint_db)
This seems to have solved my issues, as it works, and I have no errors.