一.批量替换的前提:
1.编码格式,典型有cp936格式,大都转换为utf-8
2.替换内容,最好用r–原始字符串
二.上代码:
# -*- coding: utf-8 -*-
#@author: Shinalone
page = 300
while page <= 370:
f1 = open('F:\\300-370\\'+str(page)+'.txt','r',encoding='utf-8')
f2 = open('F:\\a300-370\\'+str(page)+'.txt','w',encoding='utf-8')#写入新文件夹
for s in f1.readlines():
f2.write(s.replace(r"旧内容",'新内容'))#原始字符串
print('file_a',page,'finished')
page = page + 1
else:
print('Replacement finished!')
f1.close()
f2.close()