請問用python如何進行替換啊?
在Ubuntu下使用Python
將a.txt中所有(aaa)替換為
import os
dir_list = ['./src1','./src2']
rows_dict = dict()
rows_dict['aaa'] = 'bbb'
for dir_name in dir_list:
for file_name in os.listdir(dir_name):
if os.path.isdir(dir_name + os.path.sep + file_name):
continue
with open(dir_name + os.path.sep + file_name, 'r') as fr:
content = fr.read()
for k, v in rows_dict.iteritems():
content = content.replace(k, v)
with open(dir_name + os.path.sep + file_name, 'w') as fw:
fw.write(content)