WebLinuxStudy

导航

 

文件内容替换

# fileName 文件名

# oldStr 源字符串

# newStr 新字符串

def fileContentAlter(fileName, oldStr, newStr):
  file_data = ""
  with open(fileName, "r", encoding="utf-8") as f:
  for line in f:
    line = line.replace(oldStr, newStr)
    file_data += line
  with open(fileName, "w", encoding="utf-8") as f:
    f.write(file_data)

posted on 2020-03-03 19:16  WebLinuxStudy  阅读(701)  评论(0编辑  收藏  举报