14

I am trying to remove a file after reading from it, but getting "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"

file = open(self.filePath)
for line in file:
        #do things
file.close()

os.remove(self.filePath) #throws error

os.rename(self.filePath, self.filePath + "old") #throws same error

any ideas??

thank you!

UPDATE: i just restarted my (windows 7) box, started eclipse, moved os.remove("c:\file\file.txt") to the first line of my script, ran and got the same error.

UPDATE 2: i used windows explorer to navigate to the file.. first time I couldn't delete it, it was being used by "System". second attempt the delete worked fine. the file is no longer there. now when I run os.remove("c:\file\file.txt") WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\file\file.txt' then i look in windows explorer and the file is back, empty. i can delete in explorer, then when i run the script to remove, it throws the error (and creates the empty file). any idea what could be going on here?

UPDATE 3: i am an idiot. i was opening the file in the initialization of an object when i tested by moving the "remove" to top of script, i should have put it at the top, not after creating my objects

sorry for taking your time, i've been working on something all weekend that's due tomorrow and panicked when it wouldn't run

5
  • 2
    Grab Process Explorer or something similar and try taking a look at what actually has the file open.
    – Anon.
    Commented Dec 6, 2009 at 21:44
  • If you try to remove the file before opening it, does that work?
    – Mark Byers
    Commented Dec 6, 2009 at 21:45
  • Is filePath a directory?
    – wallyk
    Commented Dec 6, 2009 at 21:49
  • filePath is a text file. i have written to it fine, just can't remove it
    – alby
    Commented Dec 6, 2009 at 21:53
  • 1
    Hard-coding file paths is not a good idea at the best of times. Worse: great care needs to be taken on Windows where the backslash is overloaded (path separator and escape character). os.remove("c:\file\file.txt") should be os.remove(r"c:\file\file.txt") or os.remove("c:\\file\\file.txt") or os.remove("c:/file/file.txt") Commented Dec 6, 2009 at 23:04

4 Answers 4

10

Another possibility is that a virus checker still has the file open at the time you try to delete or rename it. This doesn't happen often but when it does, it's really annoying to track down.

1
  • 2
    I've also seen virus checkers screw up code like this a lot. My suggestion is to catch WindowsError and if it failed because the file is in use, sleep for a short time and try again.
    – user79758
    Commented Dec 6, 2009 at 22:33
6

Use Unlocker(easier) or Process Explorer to see what program has the file open.

(as suggested by Anon)

0
2

I have the same problem right now with an access-AB. Process Explorer shows that .. 'python.exe' is using the file that I wish to delete. I can not kill Python. It is an annoying solution.

0

There are lots of possible reasons for this. However, the most likely one is that you have a directory window open that contains the file, or that such a window was closed recently and Windows has yet to flush the directory from its cache. Try closing any directory window that you were looking in and try again.

1
  • hi.. please see update to my quesion. i just restarted windows, opened eclipse, moved the os.remove line to top of script, ran and got same error. didn't open explorer at all this windows session
    – alby
    Commented Dec 6, 2009 at 22:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.