Home > Backend Development > Python Tutorial > Detailed explanation of how to batch modify the contents of text files in Python

Detailed explanation of how to batch modify the contents of text files in Python

高洛峰
Release: 2017-03-06 13:40:12
Original
2193 people have browsed it

This article mainly introduces relevant information on how to batch modify the contents of text files in Python. Friends who need it can refer to

Python batch replaces file contents and supports nested folders

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()
Copy after login

Everyone understands the method of batch modifying text file contents in Python introduced in this article. If you have any questions, please leave me a message and the editor will promptly Reply to everyone!

For more detailed information on how to batch modify the contents of text files in Python, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template