Python批量替換文件內容,支持嵌套文件夾 ...
Python批量替換文件內容,支持嵌套文件夾
1 import os 2 path="./" 3 for root,dirs,files in os.walk(path): 4 for name in files: 5 #print name 6 if name.endswith(".html"): 7 #print root,dirs,name 8 filename=root+"/"+name 9 f=open(filename,"r") 10 filecontent="" 11 line=f.readline() 12 while line: 13 l=line.replace(":8081/arcgis_js_api","/arcgisapi") 14 filecontent=filecontent+l 15 line=f.readline() 16 f.close() 17 f2=file(filename,"w") 18 f2.writelines(filecontent) 19 f2.close()