Home  >  Article  >  Backend Development  >  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

高洛峰
高洛峰Original
2017-03-06 13:40:122008browse

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()

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!

Statement:
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 [email protected]