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
consolepip 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=Falseskips both flushes, for scratch files where durability does not matter.preserve_modecopies 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
FileNotFoundErrorbefore 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.
fsyncon the directory is skipped on Windows, whereos.replaceis already
durable and directories cannot be opened for writing.
- Temporary files are named
.<target>.<random>.tmpin the same directory, because a
rename across filesystems is not atomic. A crash in the middle leaves one behind.
Development
consolepython -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest
The suite covers every line of the package (pytest --cov --cov-branch), and the examples in the docstrings run as tests through --doctest-modules.
License
MIT.
Other libraries here
One problem each, standard library only, no runtime dependencies.
- argrepair — turn a bad tool call into a message the model can act on
- backoff-headers — turn a rate-limit header into the exact time to wait
- columncheck — find the CSV defect before the pipeline does
- dir-digest — one hash for a directory tree, identical on every OS
- docanchor — catch every broken heading anchor in your docs
- envfile-diff — diff two .env files without printing the secrets
- header-date — HTTP dates parsed strictly, formatted correctly
- junit-merge — merge JUnit XML from every job into one honest report
- npm-semver — npm semver ranges, parsed the way npm parses them
- outputcap — keep a huge tool result out of the context window
- parse-size — byte sizes in and out, MB and MiB never confused
- path-containment — keep untrusted paths inside their directory
- redactscope — stop secrets in tool results before they reach the model
- runbudget — a per-run cost cap that warns before it stops
- testflakes — rank your flaky tests from the history you already have
- tool-loop-guard — catch an agent repeating itself while it is still cheap
- toolcall-ledger — a hash-chained record of every tool call
- toolcircuit — stop retrying a tool that is down
- toolquota — client-side rate limits your agent loop can wait on
- wheeldeps — read a wheel's dependencies offline, markers included
- zip-deterministic — byte-identical archives, build after build