Skip to main content

fsync-replace

Write a file once, completely, or not at all.

from fsync_replace import write_atomic

write_atomic("report.json", '{"ok": true}')

write_atomic writes to a temporary file next to the target, calls fsync, renames it over the target with os.replace, and then fsyncs the directory entry so the rename itself is durable. A crash, a kill signal or a full disk leaves either the old file or the new one — never half of each.

The context manager is the same thing with a file handle:

from fsync_replace import atomic_open

with atomic_open("state.json", "w") as handle:
    handle.write('{"step": 3}')
    # if this block raises, state.json is untouched and the temp file is removed

Zero dependencies, Python 3.9+, works on Linux, macOS and Windows.

Why not os.replace alone

os.replace is atomic, but a temporary file that was never flushed can still be empty after a power loss, and the rename is only durable once the directory entry is flushed too. Both are done here. atomicwrites stopped at the same problem in 2022 and does not fsync the directory.

Install

pip install fsync-replace

API

write_atomic(path, data, mode="w", encoding="utf-8", *, fsync=True, newline=None, preserve_mode=True) -> int

Writes str (text modes) or bytes (mode="wb") and returns the number of characters or bytes written.

  • fsync=False skips both flushes, for scratch files where durability does not matter.
  • preserve_mode copies the permission bits of the file being replaced, so a script that was 0700 stays 0700. A new file keeps the default umask.
  • A missing parent directory raises FileNotFoundError before anything is created.

atomic_open(path, mode="w", encoding="utf-8", *, fsync=True, newline=None, preserve_mode=True)

Context manager yielding an open handle. On a clean exit the file is moved into place; on an exception the temporary file is removed and the target is left as it was. The exception is not swallowed.

Limits

  • Durability is per file. Replacing several files is not a transaction; if you need that, write a directory and swap it, or use a database.
  • fsync on the directory is skipped on Windows, where os.replace is already durable and directories cannot be opened for writing.
  • Temporary files are named .<target>.<random>.tmp in the same directory, because a rename across filesystems is not atomic. A crash in the middle leaves one behind.

Development

python -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest

License

MIT.

Metadata

Release files for fsync-replace 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fsync-replace 0.1.0
File Size Uploaded Provenance Actions
fsync_replace-0.1.0.tar.gz 6.2 kB Without provenance Details for fsync_replace-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fsync-replace 0.1.0
File Interpreter ABI Platform Actions
fsync_replace-0.1.0-py3-none-any.whl Python 3 none any Details for fsync_replace-0.1.0-py3-none-any.whl

Total release size: 11.3 kB

Release files / fsync_replace-0.1.0.tar.gz

Download URL fsync_replace-0.1.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2f65714c61dc6cab0a8489e1bd6e998d8a633ab12d7a28a383aeb0e0850fdc52
BLAKE2b-256 checksum
How to use checksums
10a6dd8771d727fff412473f3e27925e1a7425107bcc104359b140020c36d5e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / fsync_replace-0.1.0-py3-none-any.whl

Download URL fsync_replace-0.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
79c669f628564c358cb3b6d88523a30d6de8ed5abec076c32520f2b83c79c5c8
BLAKE2b-256 checksum
How to use checksums
1a6fbb98347d621c9e59fe5737770da46d20c809ef18076047535cdd856b41cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for fsync-replace 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page