@contextmanager
def atomic_writer(file_path, mode):
 temp_suffix = u'.aw.temp'
 temp_file_path = (file_path + temp_suffix)
 with open(temp_file_path, mode) as file_obj:
  try:
   (yield file_obj)
   os.rename(temp_file_path, file_path)
  finally:
   try:
    os.remove(temp_file_path)
   except (OSError, IOError):
    pass

@ contextmanager def atomic_writer ( file_path , mode ) : temp_suffix = u'.aw.temp' temp_file_path = ( file_path + temp_suffix ) with open ( temp_file_path , mode ) as file_obj : try : ( yield file_obj ) os . rename ( temp_file_path , file_path ) finally : try : os . remove ( temp_file_path ) except ( OSError , IOError ) : pass
atomic file writer .