#coding=utf-8 import os def WalkDir(dirname, *ext):# 遍历dirname目录及其子目录 for root,dirs,files in os.walk(dirname): # root 是当前目录名 # dirs 是当前目录的子目录列表 # files 是当前目录的文件列表 # print (root) for fname in files: #if os.path.splitext(fname)[1] in list(*ext): # 判断扩展名是否在列表 if fname in list(*ext): # 判断扩展名是否在列表 print (root + "\\" + fname) cont = "" f = open(root + "\\" + fname, 'rt') while True: s = f.read() if s=="": break else: #s.replace("10.20.60.59:1521:dgcms20100201", "127.0.0.1:1521:orcl") if s.find("10.20.60.59:1521:dgcms20100201") > -1: s = s.replace("10.20.60.59:1521:dgcms20100201", "127.0.0.1:1521:orcl") else: #s = s.replace("127.0.0.1:1521:orcl", "10.20.60.59:1521:dgcms20100201") s = s.replace("10.20.60.59:1521:dgcms20100201", "127.0.0.1:1521:orcl") cont +=s; f.close() #print (cont) #修改配置文件后,写回到文件中 file_object = open(root + "\\" + fname, 'wt') file_object.write(cont) file_object.close() if '__main__' == __name__: WalkDir("E:\ALL_Work\DG_Project", ['jdbc.properties', 'hibernate.cfg.xml'])