def replaceTextInFile()

in scripts/yapl/Utilities.py [0:0]


def replaceTextInFile(self, filePath, text, newText):
  """
    In the given file with path, filePath, replace text with newText.
  """
  try:
    tempPath = "%s.temp" % filePath
    backupPath = "%s.bak" % filePath
    shutil.copyfile(filePath,backupPath)
    with open(filePath,'r') as originalFile, open(tempPath,'w') as tempFile:
      for line in originalFile:
        tempFile.write(line.replace(text, newText))
      #endFor
    #endWith
    os.rename(tempPath,filePath)
    os.remove(backupPath)
  except Exception as e:
    if (os.path.exists(tempPath)): os.remove(tempPath)
    os.rename(backupPath,filePath)
    raise e