def _replace_file ( path , content ) : if os . path . exists ( path ) : with open ( path , 'r' ) as f : if content == f . read ( ) : print ( "Not overwriting {} because it is unchanged" . format ( path ) , file = sys . stderr ) return with open ( path , 'w' ) as f : f . write ( content )
python check file exists with try and write in same file
Writes a file if it doesn t already exist with the same content .
def _replace_file(path, content):
  """Writes a file if it doesn't already exist with the same content.

  This is useful because cargo uses timestamps to decide whether to compile things."""
  if os.path.exists(path):
    with open(path, 'r') as f:
      if content == f.read():
        print("Not overwriting {} because it is unchanged".format(path), file=sys.stderr)
        return

  with open(path, 'w') as f:
    f.write(content)