pythonテキストファイル全体を読み込みテキストファイルに書き込むテキストファイルの内容を置き換えます

663 ワード

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))