Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding partially written files in Python

Tags:

python

file

What is the "safest" way to write files in Python? I've heard about atomic file writing but I am not sure of how to do it and how to handle it.

like image 290
Arnaud Aliès Avatar asked Oct 16 '12 20:10

Arnaud Aliès


1 Answers

What you want is an atomic file replacement, so that there is never unfinished final file on the disk. There only exist complete new version or complete old version on the target location.

The method for Python is described here:

Atomic file replacement in Python

like image 100
Mikko Ohtamaa Avatar answered Sep 28 '22 06:09

Mikko Ohtamaa