Skip to content

karenina.utils.file_ops

file_ops

File operation utilities for Karenina.

Provides reusable atomic file write functionality.

Functions

atomic_write

atomic_write(filepath: Path, data: str) -> None

Write content to file atomically using write-rename pattern.

Writes data to a temporary partial file, then atomically renames it to the target path. This ensures the file is never in a partially written state.

Parameters:

Name Type Description Default
filepath
Path

Target file path

required
data
str

Content to write

required

Raises:

Type Description
OSError

If the write or rename fails

Source code in src/karenina/utils/file_ops.py
def atomic_write(filepath: Path, data: str) -> None:
    """Write content to file atomically using write-rename pattern.

    Writes data to a temporary partial file, then atomically renames it
    to the target path. This ensures the file is never in a partially
    written state.

    Args:
        filepath: Target file path
        data: Content to write

    Raises:
        OSError: If the write or rename fails
    """
    partial_path = filepath.with_suffix(filepath.suffix + ".partial")

    try:
        with open(partial_path, "w", encoding="utf-8") as f:
            f.write(data)
            f.flush()
            os.fsync(f.fileno())

        # Atomic rename
        partial_path.replace(filepath)

    except Exception:
        # Clean up partial file on error
        if partial_path.exists():
            with contextlib.suppress(OSError):
                partial_path.unlink()
        raise