Skip to main content

fsync-replace

PyPI Python tests license

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

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.1
File Size Uploaded Provenance Actions
fsync_replace-0.1.1.tar.gz 6.5 kB Without provenance Details for fsync_replace-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 11.6 kB

Release files / fsync_replace-0.1.1.tar.gz

Download URL fsync_replace-0.1.1.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
aa495f4426f7b286ed0dab63e9d7020f5360deecbfee5dc2ab041693a8653a31
BLAKE2b-256 checksum
How to use checksums
48838cb92ca53b27aa3b362c9cfe46a694db7d4ea1a287172618b7a52155fc6c
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.1-py3-none-any.whl

Download URL fsync_replace-0.1.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e727c12d5973f4920316b97cc7aa8c2ec2ec3cb49fd1c70962e3e17d8e8a642e
BLAKE2b-256 checksum
How to use checksums
87fd6eef2c51c4a1be2da01f99f4fcd5558587eeed014f2e3ee13b03e65ca2f6
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.1

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