Python·100 questions

How to perform atomic file write?

Answer
Write to a temporary file nearby.
fsync if needed.
Then replace/rename to target path (atomic on most FS).
Use pathlib and os.replace.
Protects against corrupted files on crash.
Was this answer helpful?

More questions in this topic

Related questions from other topics