@contextmanager
def open_atomic(filepath, *args, **kwargs):
 fsync = kwargs.pop('fsync', False)
 with _tempfile(dir=os.path.dirname(filepath)) as tmppath:
  with open(tmppath, *args, **kwargs) as f:
   (yield f)
   if fsync:
    f.flush()
    os.fsync(file.fileno())
  os.rename(tmppath, filepath)

@ contextmanager def open_atomic ( filepath , * args , ** kwargs ) : fsync = kwargs . pop ( 'fsync' , False ) with _tempfile ( dir = os . path . dirname ( filepath ) ) as tmppath : with open ( tmppath , * args , ** kwargs ) as f : ( yield f ) if fsync : f . flush ( ) os . fsync ( file . fileno ( ) ) os . rename ( tmppath , filepath )
open temporary file object that atomically moves to destination upon exiting .