#修改文件函数
def func(filename, old,new):
with open (filename,encoding = ‘utf-8‘) as f, open (‘%s.bak‘%filename, ‘w‘, encoding = ‘utf-8‘) as f2:
for line in f:
if old in line:
line = line.replace(old,new)
f2.write(line)
import os
os.remove(filename)
os.rename(‘%s.bak‘%filename,filename)
原文:https://www.cnblogs.com/farion/p/9860867.html