code: def safe_file(path, suffix=None, cleanup=True):
    
    safe_path = '{0}.{1}'.format(path, suffix or uuid.uuid4())
    if os.path.exists(path):
      shutil.copy(path, safe_path)
    try:
      yield safe_path
      if cleanup:
        shutil.move(safe_path, path)
      else:
        shutil.copy(safe_path, path)
    finally:
      if cleanup:
        safe_delete(safe_path)
docs: A with-context that copies a file, and copies the copy back to the original file on success.
  
    This is useful for doing work on a file but only changing its state on success.
  
    :param str suffix: Use this suffix to create the copy. Otherwise use a random string.
    :param bool cleanup: Whether or not to clean up the copy.