Python基于正则表达式实现文件内容替换的方法


Posted in Python onAugust 30, 2017

本文实例讲述了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('<include file="./Tpl/', '<include file="./App/Tpl/', lines, fp)
  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爬虫之HTTP异常处理
Nov 05 Python
在Python中使用SQLite的简单教程
Apr 29 Python
用C++封装MySQL的API的教程
May 06 Python
在Python中操作时间之strptime()方法的使用
Dec 30 Python
python实现分页效果
Oct 25 Python
详解用python实现简单的遗传算法
Jan 02 Python
python+ffmpeg视频并发直播压力测试
Mar 06 Python
详谈Python中列表list,元祖tuple和numpy中的array区别
Apr 18 Python
Python判断telnet通不通的实例
Jan 26 Python
pyqt5之将textBrowser的内容写入txt文档的方法
Jun 21 Python
Django实现跨域请求过程详解
Jul 25 Python
python3通过qq邮箱发送邮件以及附件
May 20 Python
Python导入模块时遇到的错误分析
Aug 30 #Python
简单学习Python多进程Multiprocessing
Aug 29 #Python
Python简单实现自动删除目录下空文件夹的方法
Aug 29 #Python
Python实现文件内容批量追加的方法示例
Aug 29 #Python
Python实现解析Bit Torrent种子文件内容的方法
Aug 29 #Python
Python 3.x读写csv文件中数字的方法示例
Aug 29 #Python
在python3环境下的Django中使用MySQL数据库的实例
Aug 29 #Python
You might like
色色整理的PHP面试题集锦
2012/03/08 PHP
php实现的统计字数函数定义与使用示例
2017/07/26 PHP
Prototype使用指南之array.js
2007/01/10 Javascript
jQuery中json对象的复制方式介绍(数组及对象)
2013/06/08 Javascript
cookie.js 加载顺序问题怎么才有效
2013/07/31 Javascript
异步动态加载JS并运行(示例代码)
2013/12/13 Javascript
javascript得到当前页的来路即前一页地址的方法
2014/02/18 Javascript
javascript单引号和双引号的区别和处理
2014/05/14 Javascript
使用jquery制作弹出框效果
2015/04/03 Javascript
angularjs学习笔记之双向数据绑定
2015/09/26 Javascript
简单实现JS倒计时效果
2016/12/23 Javascript
jQuery实现select模糊查询(反射机制)
2017/01/14 Javascript
js模块加载方式浅析
2017/08/12 Javascript
Vue-Router实现组件间跳转的三种方法
2017/11/07 Javascript
搭建element-ui的Vue前端工程操作实例
2018/02/23 Javascript
Bootstrap4 gulp 配置详解
2019/01/06 Javascript
JS实现进度条动态加载特效
2020/03/25 Javascript
openlayers实现地图弹窗
2020/09/25 Javascript
python实现的各种排序算法代码
2013/03/04 Python
python获取网页状态码示例
2014/03/30 Python
python中精确输出JSON浮点数的方法
2014/04/18 Python
Python常见异常分类与处理方法
2017/06/04 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
Python semaphore evevt生产者消费者模型原理解析
2020/03/18 Python
python3中for循环踩过的坑记录
2020/12/14 Python
Adobe Html5 Extension开发初体验图文教程
2017/11/14 HTML / CSS
工商企业管理应届生求职信
2013/11/03 职场文书
实习心得体会
2014/01/02 职场文书
多媒体专业自我鉴定
2014/02/28 职场文书
中班幼儿评语大全
2014/04/30 职场文书
优秀毕业生求职信
2014/06/05 职场文书
交通安全责任书范本
2014/07/24 职场文书
稽核岗位职责范本
2015/04/13 职场文书
爱国教育主题班会
2015/08/14 职场文书
2016年教师党员创先争优承诺书
2016/03/24 职场文书
如何写好活动总结
2019/06/21 职场文书