pythonfilesystemsrenameatomicfwrite

How to do atomic file replacement?


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.)


Solution

  • Create a new file (on the same filesystem) and use os.replace() to replace that over an existing file.

    import os
    os.replace('source', 'destination')
    

    The advantage of os.replace() over os.rename() is that it will also atomically overwrite the destination on Windows.