Back to results for “JSON”
Python › File I/O & Context Managers

How would you write a context manager that atomically writes to a file, avoiding leaving a corrupted or partially-written file if an exception occurs mid-write?

Advanced

Answer

Write to a TEMPORARY file first, and only rename it to the FINAL destination (an atomic OS-level operation) in __exit__ if NO exception occurred — if an exception did occur, clean up the temp file instead, ensuring the original (or no) file is ever left in a partially-written, corrupted state.
import os, tempfile
from contextlib import contextmanager

@contextmanager
def atomic_write(path):
    tmp_fd, tmp_path = tempfile.mkstemp(dir=os.path.dirname(path) or ".")
    try:
        with os.fdopen(tmp_fd, "w") as f:
            yield f
        os.replace(tmp_path, path)  # atomic rename, only on success
    except Exception:
        os.remove(tmp_path)
        raise

with atomic_write("config.json") as f:
    json.dump(data, f)
Real-world example Safely updating a critical configuration file that other processes might read concurrently, without ever exposing a half-written state.

Common follow-ups: Why is os.replace() specifically chosen here over a plain rename or direct write to the final path?