Python批量修改文本文件内容的方法

yipeiwu_com6年前Python基础

Python批量替换文件内容,支持嵌套文件夹

import os
path="./"
for root,dirs,files in os.walk(path):
for name in files:
#print name
if name.endswith(".html"):
#print root,dirs,name 
filename=root+"/"+name
f=open(filename,"r")
filecontent=""
line=f.readline() 
while line:
l=line.replace(":/arcgis_js_api","/arcgisapi")
filecontent=filecontent+l
line=f.readline()
f.close()
f=file(filename,"w")
f.writelines(filecontent)
f.close()

关于本文给大家介绍的Python批量修改文本文件内容的方法,大家都看懂了,如果大家有疑问欢迎给我留言,小编会及时给大家回复的!

相关文章

python print 按逗号或空格分隔的方法

1)按,分隔 a, b = 0, 1 while b < 1000: print(b, end=',') a, b = b, a+b 1,1,2,3,5,8,13,...

利用Python正则表达式过滤敏感词的方法

利用Python正则表达式过滤敏感词的方法

问题描述:很多网站会对用户发帖内容进行一定的检查,并自动把敏感词修改为特定的字符。 技术要点: 1)Python正则表达式模块re的sub()函数; 2)在正则表达式语法中,竖线“|”表...

Python全排列操作实例分析

本文实例讲述了Python全排列操作。分享给大家供大家参考,具体如下: step 1: 列表的全排列: 这个版本比较low # -*-coding:utf-8 -*- #!pytho...

django传值给模板, 再用JS接收并进行操作的实例

今天用要django传值给模板, 然后需要用js处理一下.特此记录. 用json.dumps()方法将值传给模板. import json return render(reques...

Python txt文件加入字典并查询的方法

如下所示: dicFile = open('train_1.txt', 'r')#打开数据   print '开始装载数据...