[Python-ideas] BackupFile
Christian Heimes
lists at cheimes.de
Mon Jun 25 15:59:40 CEST 2012
Am 25.06.2012 14:17, schrieb Kim Gräsman:
> Hello,
>
> I'm new here, so forgive me if this has been discussed before or is off-topic.
>
> I came up with a mechanism that I thought might be useful in the
> Python standard library -- a scope-bound self-restoring backup file. I
> came to this naïve implementation;
Are you aiming for atomic file rollover backed by a temporary file?
That's the common way to safely overwrite an existing file. It works
differently than your code.
* Create a temporary file with O_CREAT | O_EXCL in the same directory as
the file you like to replace
* Write data to new file
* Call sync() on the file as well as fdatasync() and fsync() on the file
descriptor
* close the file
* use atomic rename to replace the old file with the new file (IIRC
won't work atomically on Windows)
I've some code laying around somewhere that implements a RolloverFile
similar to tempfile.NamedTemporaryFile.
Christian
More information about the Python-ideas
mailing list