What's the recommended way to replace a file atomically in Python?
i.e. if the Python script is interrupted, there is a power outage etc. files do not have a high probability of ending up in an inconsistent state (half written to the disk).
A solution for Linux/UNIX platforms is preferred.
(I know getting 100% atomic operations might depend on your file system, but at least make the chances for the corruption low.)
Atomic renameThe rename function from the C library in Windows does not implement the POSIX atomic behaviour; instead it fails if the destination file already exists. However, other calls in the Windows API do implement the atomic behaviour.
replace() method in Python is used to rename the file or directory. If destination is a directory, OSError will be raised. If the destination exists and is a file, it will be replaced without error if the action performing user has permission.
For any individual file, the move or rename performed by mv is atomic provided that the file is moved within the same filesystem.
An atomic operation is one that changes a system from one state to another without visibly passing through any intermediate states. Atomicity is desirable when altering the content of a file because: The process performing the alteration may fail or be stopped, leaving the file in an incomplete or inconsistent state.
Create a new file and os.rename() it over the existing file. This is atomic on most platforms under most conditions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With