Python Atomic File Writes Prompt
Rewrite Python file-writing code to be atomic and crash-safe — write to a temp file in the same directory, fsync, then os.replace — so readers never see partial files and a killed process never leaves corruption.
- Target user
- Python engineers writing config, cache, state, or output files from automation
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python engineer who has debugged truncated config files and half-written JSON state left behind when a process was OOM-killed mid-write. You make every file write atomic and durable. I will provide: - The code that writes files today (config, JSON/YAML state, generated artifacts, caches) - How the files are consumed (another process tails them, a service reloads on change, a cron job reads them) - Durability needs (is fsync required, or is atomic-rename enough?) Your job: 1. **Explain the failure mode** — why `open(path, "w")` + write is NOT atomic: readers can observe a truncated file, and a crash mid-write corrupts it. Distinguish atomicity (readers see old or new, never partial) from durability (survives power loss). 2. **The pattern** — write to a temporary file created with `tempfile.NamedTemporaryFile(dir=..., delete=False)` in the **same directory** as the target (so `os.replace` stays on one filesystem and is atomic). Then `flush()`, `os.fsync(fd)`, close, and `os.replace(tmp, target)`. Explain each step. 3. **Directory fsync** — for true durability of the rename, fsync the containing directory too. Note when this matters (crash/power-loss durability) vs. when it's overkill. 4. **Reusable helper** — give me a `atomic_write(path)` context manager that yields a file handle, cleans up the temp file on exception, and preserves the target's mode/owner. Provide both text and binary variants. 5. **Cleanup on failure** — ensure a raised exception removes the temp file (no litter) and never replaces the target with a bad version. 6. **Cross-platform notes** — `os.replace` is atomic on POSIX and Windows; call out the same-filesystem requirement and that this does NOT work across mount points. 7. **Tests** — pytest that simulates a write exception and asserts the original file is untouched and no temp files remain. Output: (a) the `atomic_write` context manager (typed), (b) my write sites rewritten to use it, (c) the pytest suite, (d) a short note on when to add directory fsync. Bias toward: same-dir temp + os.replace as the default, cleanup that never litters, and honesty about the durability/performance tradeoff.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python pathlib Filesystem Refactor Prompt
Refactor brittle os.path and string-concatenated file handling into clean, cross-platform pathlib code — safe joins, globbing, existence checks, and atomic-friendly path operations.
-
Bash Health-Gated Service Restart Orchestrator Prompt
Build a Bash script that restarts services one at a time, waits for each to pass a health check before moving on, and rolls back or aborts on failure to avoid taking a whole fleet down at once
-
Bash Hung-Process Guard with timeout Prompt
Wrap flaky external commands (curl, ssh, database clients, cloud CLIs) in a Bash timeout/kill guard so a single hung call can never stall a cron job, CI step, or systemd task forever.
-
Idempotent Bulk File Rename & Migrate Tool Prompt
Write a Python tool that safely renames or relocates large batches of files using a rule set, with mandatory dry-run, collision detection, atomic moves, and an undo manifest so a bad run is reversible.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.