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 ...
随机推荐
- git如何利用分支进行多人开发
在使用git时,假如远程仓库有 dev 和 master 两个分支,master 作为一个稳定版分支,可用于直接发布产品,日常的开发则 push 到 dev 分支,那本地是不是要从 dev 分支中创建 ...
- 1. let与const
1.ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. var a = []; for (var i = 0;i<10;i++) ...
- sysobjects统计数据库的各类结构数据
概要: 查询表.以及表中的行数 SELECT a.name,b.rows FROM sysobjects a INNER JOIN sysindexes b ON a.id=b.id WHERE a. ...
- icpc 银川 H. Delivery Route SPFA优化
Problem Description Pony is the boss of a courier company. The company needs to deliver packages to ...
- 2019年京东 PHP工程师面试题
1. Apache与Nginx大访问下性能描述正确的是? A.Apache所采用的epoll网络I/O模型非常高效B.Nginx使用了最新的kqueue和select网络I/O模型C.Apache进程 ...
- vue 的 watch 如何在初始化时执行
之前的做法一直是在 created 钩子之后手动调用一次 created() { this.fetchText(); }, watch: { text: 'fetchText', } 后来在翻阅文档的 ...
- LSTM-航班人数预测
小书匠深度学习LSTM 郑重声明,文章大部分翻译自: Time Series Prediction with LSTM Recurrent Neural Networks in Python with ...
- pandas 筛选某一列最大值最小值 sort_values、groupby、max、min
高效方法: dfs[dfs['delta'].isnull()==False].sort_values(by='delta', ascending=True).groupby('Call_Number ...
- codeforces396A
sol:很显然就是找出所有质因数,然后分别塞进去就行了,怎么塞就是组合数.感觉就是道小学奥数题 #include <bits/stdc++.h> using namespace std; ...
- Ubuntu 源 (ros)
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://archive. ...