0

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.

Cody Dostal
  • 311
  • 1
  • 3
  • 13
  • 2
    It looks like you're building your filename/path incorrectly - that exception text is showing two filenames concatenated together (`c:\...\complaints.dbc:\...\complaints.db.backup`) and I expect there should only be one there. – nobody Mar 31 '14 at 18:27
  • @AndrewMedico: That is true. I fixed that problem. – Cody Dostal Apr 01 '14 at 16:19
  • @eryksun: I fixed the original error, but the there are no other running instances of my script and the file does not exist. This is a screenshot of the working directory: http://imgur.com/M9nk41s As you can see, complaints.db.bak does not exist. I'm not sure why this is happening. It's not read only, no other instances of my software running, and I changed all of my original `open` statements to `with open` statements. – Cody Dostal Apr 01 '14 at 16:21
  • Also, even running cmd as administrator, and then running the program with that doesn't work. And in my earlier comment, by "original error", I meant the first error. – Cody Dostal Apr 01 '14 at 16:27
  • Sorry for all the comments in a row. Can't edit after 5 minutes. Restarting doesn't fix the issue, leading me to believe it is somewhere in my code. Is there not a universal way to close a file if it is open? Like what unlocker (the software) does? The weirdest part is I can still move the db whenever I want, and unlocker shows no locker handle, even after my program halts due to one. – Cody Dostal Apr 01 '14 at 16:51

1 Answers1

1

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.

Cody Dostal
  • 311
  • 1
  • 3
  • 13
  • Try `os.replace(complaint_db + "2", complaint_db)` instead of `shutil.move`, to see whether it raises a `PermissionError`. – Eryk Sun Apr 02 '14 at 03:33
  • @eryksun it actually was one of the ones that did raise a permission error. It raises "WinError 5" `builtins.PermissionError: [WinError 5] Access is denied: 'c:\\Users\\Cody\\Documents\\MEGA\\ComplaintManagement\\complaints.db'` – Cody Dostal Apr 03 '14 at 16:55
  • Then complaints.db is definitely open. In this case, `shutil.move` works by copying the contents of complaints.db2 to complaints.db. This is allowed (assuming complaints.db isn't locked), while an outright `rename`, `replace`, or `unlink` (delete) is forbidden in this case. The copy operation isn't atomic, so I think you should be concerned about data corruption. – Eryk Sun Apr 03 '14 at 21:46
  • eryksun: That's a bit disheartening. I can't find anywhere that it is still open in my code, and nothing else has it open. Is there a way to verify if it is open? If so, is there a way to force close it? – Cody Dostal Apr 05 '14 at 01:43