def atomic_open(filename, mode='w'):
 if (mode in ('r', 'rb', 'r+', 'rb+', 'a', 'ab')):
  raise TypeError("Read or append modes don't work with atomic_open")
 ntf = tempfile.NamedTemporaryFile(mode, prefix='.___atomic_write', dir=os.path.dirname(filename), delete=False)
 return _AtomicWFile(ntf, ntf.name, filename)

def atomic_open ( filename , mode = 'w' ) : if ( mode in ( 'r' , 'rb' , 'r+' , 'rb+' , 'a' , 'ab' ) ) : raise TypeError ( "Read or append modes don't work with atomic_open" ) ntf = tempfile . NamedTemporaryFile ( mode , prefix = '.___atomic_write' , dir = os . path . dirname ( filename ) , delete = False ) return _AtomicWFile ( ntf , ntf . name , filename )
works like a regular open() but writes updates into a temporary file instead of the given file and moves it over when the file is closed .