Intermittent "permission denied" errors when using os.rename and a recently deleted path??

Russell Warren russandheather at gmail.com
Wed Jul 26 17:21:46 EDT 2006


I've been having a hard time tracking down a very intermittent problem
where I get a "permission denied" error when trying to rename a file to
something that has just been deleted (on win32).

The code snippet that gets repeatedly called is here:

  ...
  if os.path.exists(oldPath):
    os.remove(oldPath)
  os.rename(newPath, oldPath)
  ...

And I get the permission denied exception on the os.rename line.
Somehow the rename target is still locked?  I don't get it.

I found a post that seemed to refer to precisely this problem:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/496625ca3b0c3874/e5c19db11d8b6d4e?lnk=gst&q=os.remove+delay&rnum=1#e5c19db11d8b6d4e

However - this post describes a case where there are multiple threads
making use of other os calls.  I am running a single threaded
application and still getting this problem.  ie: the suggested fix does
not work for me.

I'm trying to see if implementing a "trap the exception and try again,
but not too many times" hack fix will do the trick, but I'm not a big
fan of this "solution", and at this point I'm not entirely certain it
will work because confirming that it *did* work is tough (it is very
difficult to repeatably create the problem).

Does anyone know of a real solution to this problem, or know what
exactly is happening so that I can work out a proper solution?

Thanks,
Russ




More information about the Python-list mailing list