# -*- coding: utf-8 -*-
import os
from = "aaa"
#替換為什麼內容,可以是多行
to = """xxx
yyy
zzz"""
def handle(rootDir):
list_dirs = os.walk(rootDir)
for root, dirs, files in list_dirs:
for d in dirs:
#print os.path.join(root, d)
pass
for f in files:
do_replace(os.path.join(root, f))
def do_replace(fileName):
#格式過濾
if not fileName.endswith(".htm") and not fileName.endswith(".html"):
return
print fileName
f=open(fileName,'r')
data=f.read()
#文本替換
data=data.replace(from, to)
f.close()
f=open(fileName,'w')
f.write(data)
f.close()
#處理指定的目錄
handle("/home/xxx/yyy")