Can os.remove followed by os.path.isfile disagree?

ppaterson at gmail.com ppaterson at gmail.com
Wed Jun 6 10:35:47 EDT 2007


Can os.path.isfile(x) ever return True after os.remove(x) has
successfully completed? (Windows 2003,  Python 2.3)

We had a couple of failures in a server application that we cannot yet
reproduce in a simple case. Analysis of the code suggests that the
only possible explanation is that the following occurs,

os.remove(x)
.... stuff
if os.path.isfile(x):
    raise "Ooops, how did we get here?"
file(x, "wb").write(content)

We end up in the raise. By the time we get to look at the system the
file is actually gone, presumably because of the os.remove.

The "stuff" is a handful of lines of code which don't take any
significant time to perform. There are no "try" blocks to mask a
failure in the os.remove call.


The application is multithreaded so it is possible that another thread
writes to the file between the "remove" and the "isfile", but at the
end of the failure the file is actually not on the filesystem and I
don't believe there is a way that the file could be removed again in
this scenario.

Regards,

Paul




More information about the Python-list mailing list