Safely renaming a file without overwriting

Wolfgang Draxinger wdraxinger at darkstargames.de
Sat Oct 28 12:02:51 EDT 2006


Diez B. Roggisch wrote:

>> 1: Open the file with os.open
>> 
>> 2: Lock the file exclusively -> no other process can now
>> access it.
>> 
>> 3: Use rename to rename the file; this causes a file system
>> level implicit unlink of the old file (it dissappears from the
>> file system) but the opening process can still access it.
>> 
>> 4: close the file -> the lock is removed and the rename
>> finalized.

The open is to happen on the new file name with O_CREAT | O_EXCL
flags. Sorry, I forgot that to mention explicitly. However I
have not tried it yet, but it should work.

Wolfgang Draxinger
-- 
E-Mail address works, Jabber: hexarith at jabber.org, ICQ: 134682867
GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E



More information about the Python-list mailing list