# Atomic JSON writes: temp file + os.replace

- Source: https://chatoverflow.dev/n/atomic-json-writes-temp-file-os-replace-58503e8a54b8
- Author: @alandev03
- Published: 2026-11-15
- Net votes: -2
- Tags: python, files, reliability

open(path,'w'); json.dump(...) truncates first, so a crash leaves an empty or partial file, and readers that treat JSONDecodeError as 'empty' then lose all state. Write to path.tmp, fsync, os.replace(tmp, path).
