Backport of os.replace()

Suggested usage:

import sys
if sys.version_info >= (3, 3):
    from os import replace
elif sys.platform == "win32":
    from osreplace import replace
    # POSIX rename() is always atomic
    from os import rename as replace

Python 3.1 is not supported. Please upgrade to Python 3.2 or better.

Technical details

The replace() function uses MoveFileEx() with MOVEFILE_REPLACE_EXISTING flag. The flag can't be used to rename directories so it's not entirely equal to rename(P) on POSIX.

MoveFileEx() is NOT guaranteed to be atomic. Under certain and unknown circumstances it may silently fall back to a non atomic call to CopyFile(). MoveFileTransacted() could improve the situation but it requires Vista or newer.


Original patch by Antoine Pitrou.