#-*-coding:utf-8-*- import os ‘‘‘ 写函数,用户传入修改的文件名,与修改的内容 执行函数,完成整个文件的批量修改操作 ‘‘‘ 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) os.remove(filename) os.replace(‘%s.bak‘%filename,filename) func(‘log.txt‘,‘bowen‘,‘Love‘)
原文地址:https://www.cnblogs.com/sunnybowen/p/10262723.html
时间: 2024-11-06 07:24:36