def _fancy_rename(oldname, newname):
 if (not os.path.exists(newname)):
  try:
   os.rename(oldname, newname)
  except OSError:
   raise
  return
 try:
  (fd, tmpfile) = tempfile.mkstemp('.tmp', prefix=(oldname + '.'), dir='.')
  os.close(fd)
  os.remove(tmpfile)
 except OSError:
  raise
 try:
  os.rename(newname, tmpfile)
 except OSError:
  raise
 try:
  os.rename(oldname, newname)
 except OSError:
  os.rename(tmpfile, newname)
  raise
 os.remove(tmpfile)

def _fancy_rename ( oldname , newname ) : if ( not os . path . exists ( newname ) ) : try : os . rename ( oldname , newname ) except OSError : raise return try : ( fd , tmpfile ) = tempfile . mkstemp ( '.tmp' , prefix = ( oldname + '.' ) , dir = '.' ) os . close ( fd ) os . remove ( tmpfile ) except OSError : raise try : os . rename ( newname , tmpfile ) except OSError : raise try : os . rename ( oldname , newname ) except OSError : os . rename ( tmpfile , newname ) raise os . remove ( tmpfile )
rename file with temporary backup file to rollback if rename fails .