Welcome, guest | Sign In | My Account | Store | Cart
from contextlib import contextmanager
import os, binascii

@contextmanager
def replacefile(name):
    tmpname
= '%s.tmp-%s' % (name, binascii.hexlify(os.urandom(10)).decode('ascii'))
   
try:
       
with open(tmpname, 'w+') as f:
           
yield f
        os
.rename(tmpname, name)
   
finally:
       
try:
            os
.unlink(tmpname)
       
except OSError:
           
pass

Diff to Previous Revision

--- revision 2 2012-06-17 11:55:57
+++ revision 3 2012-06-17 12:09:49
@@ -7,7 +7,7 @@
     
try:
         
with open(tmpname, 'w+') as f:
             
yield f
-            os.rename(tmpname, name)
+        os.rename(tmpname, name)
     
finally:
         
try:
             os
.unlink(tmpname)

History