Python基于正则表达式实现文件内容替换的方法
本文实例讲述了Python基于正则表达式实现文件内容替换的方法。分享给大家供大家参考,具体如下:
最近因为有一个项目需要从普通的服务器移植到SAE,而SAE的thinkphp文件结构和本地测试的有出入,需要把一些html和js的引用路径改成SAE的形式,为了不手工改,特地速成了一下Python的正则表达式和文件操作。主要要求是将某目录下的html和js里面的几个路径变量分别更改成相应的形式,匹配文件名的时候用了正则
import os
import re
#all file in the directory
filelist = []
#function to traverse the directory
def recurseDir(path):
for i in os.listdir(path):
if os.path.isdir(path '\\' i):
recurseDir(path '\\' i)
else:
p = path '\\' i
print p
filelist.append(p)
#replace the file content
def replace(strFind, strReplace, lines, fileIO):
for s in lines:
if s.find(strFind) != -1:
foutput.write(s)
fileIO.write(s.replace(strFind,
strReplace))
rootpath = os.path.abspath('.')
recurseDir(rootpath)
pattern1 = re.compile(r'. html')
pattern2 = re.compile(r'. js')
for fileName in filelist:
match1 = pattern1.match(fileName)
match2 = pattern2.match(fileName)
if match1 or match2:
lines = open(fileName).readlines()
fp = open(fileName '.temp','w')
foutput = open("result.txt", 'w')
foutput.write(fileName)
if match1:
replace('
if match2:
replace('xxx/index.php',
'index.php', lines, fp)
fp.close()
#delete original file
if os.path.exists(fileName):
os.remove(fileName);
#rename the temp file
os.rename(fileName '.temp', fileName)
Python基于正则表达式实现文件内容替换的方法的更多相关文章
- python 修改文件内容3种方法
原文链接:https://www.cnblogs.com/wc-chan/p/8085452.html def alter(file,old_str,new_str): ""&qu ...
- python字符串内容替换的方法(转载)
python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法. ...
- linux批量替换文件内容3种方法(perl,sed,shell)
方法1:perl 这两天在构建一个应用的使用用到了maven,由于project很大,足足有700多个 pom.xml文件,更郁闷的是在很多pom.xml文件里都单独指定了资源库的url,我需要把 ...
- python中readline判断文件读取结束的方法
注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...
- sed命令实现文件内容替换总结案例
sed -i "s@AAAAA@BBBBB@g" /home/local/payment-biz-service/env/test.txt sed -i "s#htxk. ...
- Python中读取csv文件内容方法
gg 224@126.com 85 男 dd 123@126.com 52 女 fgf 125@126.com 23 女 csv文件内容如上图,首先导入csv包,调用csv中的方法reader()创建 ...
- python 比较2个文件内容
1. 通过使用md5字符串比较2个文件 import hashlib def get_file_md5(filename): '''可以比较两个文件的md5值,来比较文件内容.未使用''' md5 = ...
- Shell脚本实现乱序排列文件内容的多种方法(洗牌问题)
洗牌问题:洗一副扑克,有什么好办法?既能洗得均匀,又能洗得快?即相对于一个文件来说怎样高效率的实现乱序排列? ChinaUnix 确实是 Shell 高手云集的地方,只要你想得到的问题,到那里基本上都 ...
- python自动化之上传文件的3种方法和图片预览
ajax上传文件的三种方法 第一种xhr提交 function xhrSubmit(){ // $('#fafafa')[0] var file_obj = document.getElementBy ...
随机推荐
- Cache 和 Buffer 的区别在哪里
Cache和Buffer是两个不同的概念,简单的说,Cache是加速“读”,而buffer是缓冲“写”,前者解决读的问题,保存从磁盘上读出的数据,后者是解决写的问题,保存即将要写入到磁盘上的数据.在很 ...
- python_面向对象——多态
1.同一接口,多种形态 class Document: def __init__(self,name): self.name = name def show(self): # 异常处理:提示子类必须把 ...
- win10 水晶报表安装包
windows 10 64 VS2013安装 CR For VS 13_0_18 安装过程没有报错 安装成功http://downloads.businessobjects.com/akdlm/cr4 ...
- PHP yield占用内存测试
function com($start) { $tmp = []; for($i=0; $i<300000; $i++){ $tmp[] = $i; } $end = memory_get_us ...
- web+页面支持批量下载吗
一.此方法火狐有些版本是不支持的 window.location.href = 'https://*****.oss-cn-**.aliyuncs.com/*********';二.为了解决火狐有些版 ...
- CF1051D Bicolorings 递推
考试T2,随便推一推就好了~ code: #include <bits/stdc++.h> #define N 1015 #define mod 998244353 #define ll ...
- QLocalSocket
QIODevice做为QLocalSocket的父类 在Qt中,提供了多种IPC方法.看起来好像和Socket搭上点边,实则底层是windows的name pipe.这应该是支持双工通信的 QLoca ...
- URL中的String参数问题
测试一个查询数据的接口,类似这样的URL:.../search?type=Astring,在浏览器中输入URL获取到的数据为空,但通过其它方式确认数据库中确实已有数据,怀疑是接口实现问题.找接口实现的 ...
- 在其他博客里看到的比较好的map用法,进行储存啦啦~ x
1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...
- codeforces514E
Darth Vader and Tree CodeForces - 514E When Darth Vader gets bored, he sits down on the sofa, closes ...