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

yipeiwu_com5年前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批量修改文本文件内容的方法,大家都看懂了,如果大家有疑问欢迎给我留言,小编会及时给大家回复的!

相关文章

Django中在xadmin中集成DjangoUeditor过程详解

Django中在xadmin中集成DjangoUeditor过程详解

环境 python版本:3.6 django:1.10.8 1.下载xadmin https://github.com/sshwsfc/xadmin 下载DjangoUeditor ht...

Python使用pylab库实现绘制直方图功能示例

Python使用pylab库实现绘制直方图功能示例

本文实例讲述了Python使用pylab库实现绘制直方图功能。分享给大家供大家参考,具体如下: Python直方图 #!/usr/bin/python # -*- coding: u...

Window10+Python3.5安装opencv的教程推荐

Window10+Python3.5安装opencv的教程推荐

1.确定Python版本,电脑64位或者32位 打开cmd(window键+R,输入cmd就出现),在命令行输入:打开cmd(window键+R,输入cmd就出现),在命令行输入:pyt...

利用Python如何生成便签图片详解

利用Python如何生成便签图片详解

前言 最近有文字转图片的需求,但是不太想下载 APP,就使用 Python Pillow 实现了一个,效果如下: PIL 提供了 PIL.ImageDraw.ImageDraw.te...

python3安装speech语音模块的方法

python3安装speech语音模块的方法

在windows平台上使用pyhton编写语音识别程序需要用到speech模块,speech模块支持的主要功能有:文本合成语音,将键盘输入的文本信息转换为语音信号方式输出;语音识别,将输...