Skip to main content

fsync-replace: replace a file so a crash cannot leave it half-written

PyPI version Python versions Downloads CI License docs zero dependencies

replace a file so a crash cannot leave it half-written

Standard library only. No runtime dependencies. Python 3.9+.

how fsync-replace fits in

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

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

Built distribution (wheel)

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

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

Download URL fsync_replace-0.1.3-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8097dcc5328fb01a5a1085bb726e0350788161f3008060b51ef142e5d0c85d92
BLAKE2b-256 checksum
How to use checksums
dae1c1a08f027ce89b13a49e75b8db1da8582f25fee947d89229ad6e3cfe3226
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.14

Security for fsync-replace 0.1.3

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