Replacing Atomic Files in Python

What is 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 are not likely to be in an inconsistent state (half written to disk).

The preferred solution is for Linux / UNIX platforms.

(I know that getting 100% atomic operations may depend on your file system, but at least it reduces the likelihood of low corruption)

+7
source share
1 answer

Create a new file and os.rename () above the existing file. It is atomic on most platforms in most conditions .

+11
source

All Articles