Atomic File Write With Temp File And Rename

An atomic file writer that stages to a temporary file in the same directory, fsyncs, and renames so readers never observe partial content.

PythonCli Tooling

Code

import os
import tempfile
from pathlib import Path
from typing import Union


class AtomicWriteError(RuntimeError):
    """Raised when the atomic write could not be completed."""


def atomic_write(path: Union[str, Path], payload: Union[bytes, str], mode: int = 0o644) -> None:
    """Write payload to path atomically via same-directory temp file and rename."""
    dest = Path(path)
    parent = dest.parent
    if not parent.is_dir():
        raise AtomicWriteError(f"destination directory does not exist: {parent}")
    data = payload.encode("utf-8") if isinstance(payload, str) else payload
    fd, tmp_name = tempfile.mkstemp(dir=str(parent), prefix=f".{dest.name}.", suffix=".tmp")
    tmp = Path(tmp_name)
    try:
        with os.fdopen(fd, "wb") as handle:
            handle.write(data)
            handle.flush()
            os.fsync(handle.fileno())
        os.chmod(tmp, mode)
        os.replace(tmp, dest)
    except OSError as exc:
        tmp.unlink(missing_ok=True)
        raise AtomicWriteError(f"atomic write failed for {dest}: {exc}") from exc