python读取整个文本文件 写入文本文件 替换文本文件中的内容

import pickle

def ReadAllContent(path):
  f = open(path,"r")
  txt = f.read()
  f.close()
  return txt

def WriteAllContent(path, content):
  file = open(path,"w")
  file.write(content)
  file.close()

def UpdateFile(path,targetContent,replaceContent):
  """
  read file
  if find
    replace
    write to file
  """
  print("src file: {0}".format(path))
  content = ReadAllContent(path)
  contentIndex = content.find(targetContent)
  print("contentIdnex: {0}".format(contentIndex))
  if contentIndex > 0:
    WriteAllContent(path, content.replace(targetContent,replaceContent))

你可能感兴趣的:(python)