python 批量处理文件夹下的文件 替换文本

import re
import os

files = os.listdir('./')

def replaceStr(file):
    with open(file, 'r', encoding='utf-8') as f:
        str = f.read()
        print(str)
        str1 = re.sub('a', '我是替换的', str)
        with open(file,'w', encoding='utf-8') as f:
            f.write(str1)

print(files)
for f in files:
    if f.find('.txt') > -1 :
        replaceStr(f)

你可能感兴趣的:(Python)