Message 106587 - Python tracker (original) (raw)

os.rename() is atomic on Linux, but on Windows it raises an error if the destination does already exist.

Not atomic pseudo-code for Windows: if exists(b): unlink(b) rename(a, b)

Windows offers different functions depending on the version:

I don't think that it's possible to write an atomic rename (file) function for any OS, so it's only a "best effort" atomic function. The documentation will give a list of OS on which the operation is atomic (eg. Linux).

Note: os.rename() uses MoveFileW() on Windows.