Skip to content
CloudOps
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Intermediate ClaudeChatGPT

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

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 1,603 DevOps AI prompts
  • One practical workflow email per week