def secure_dump(object_, path, dump_function=dump, **kwargs):
 try:
  logger.debug('Dumping object to a temporary file')
  with tempfile.NamedTemporaryFile(delete=False, dir=config.temp_dir) as temp:
   dump_function(object_, temp, **kwargs)
  logger.debug('Moving the temporary file')
  shutil.move(temp.name, path)
  logger.debug('Dump finished')
 except:
  if ('temp' in locals()):
   os.remove(temp.name)
  raise

def secure_dump ( object_ , path , dump_function = dump , ** kwargs ) : try : logger . debug ( 'Dumping object to a temporary file' ) with tempfile . NamedTemporaryFile ( delete = False , dir = config . temp_dir ) as temp : dump_function ( object_ , temp , ** kwargs ) logger . debug ( 'Moving the temporary file' ) shutil . move ( temp . name , path ) logger . debug ( 'Dump finished' ) except : if ( 'temp' in locals ( ) ) : os . remove ( temp . name ) raise
robust serialization - does not corrupt your files when failed .