@contextlib.contextmanager
def remove_path_on_error(path):
 try:
  (yield)
 except Exception:
  with excutils.save_and_reraise_exception():
   delete_if_exists(path)

@ contextlib . contextmanager def remove_path_on_error ( path ) : try : ( yield ) except Exception : with excutils . save_and_reraise_exception ( ) : delete_if_exists ( path )
protect code that wants to operate on path atomically .