我想用 Python 遍历当前文件夹下的所有子目录, 并把里面所有的 Test.f03 文件里面的 Source 替换成 Target
# encoding: utf-8
import re
import os
import sys
filepath = []
def AlterContent(filepath):
Target_File = 'Test.f03'
FileOpen = open (Target_File,'r')
w_str = ""
for line in FileOpen:
if re.search ('Source', line):
line = re.sub ('Source', 'Target', line)
w_str += line
else:
w_str += line
print (w_str)
WriteOpen = open (Target_File,'w')
WriteOpen.write (w_str)
FileOpen.close ()
WriteOpen.close ()
AlterContent(filepath)
遍历目录的部分怎么写呢.