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的Template使用指南
Sep 11 Python
使用Python中PDB模块中的命令来调试Python代码的教程
Mar 30 Python
python实现将文本转换成语音的方法
May 28 Python
Win7下搭建python开发环境图文教程(安装Python、pip、解释器)
May 17 Python
python实现批量监控网站
Sep 09 Python
python常用函数详解
Sep 13 Python
Python实现的微信公众号群发图片与文本消息功能实例详解
Jun 30 Python
Python之web模板应用
Dec 26 Python
解决使用pycharm提交代码时冲突之后文件丢失找回的方法
Aug 05 Python
Python调用adb命令实现对多台设备同时进行reboot的方法
Oct 15 Python
使用Python制作缩放自如的圣诞老人(圣诞树)
Dec 25 Python
tensorflow实现读取模型中保存的值 tf.train.NewCheckpointReader
Feb 10 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中ftp_chdir与ftp_cdup函数用法
2014/11/18 PHP
php实现删除空目录的方法
2015/03/16 PHP
Three.js源码阅读笔记(基础的核心Core对象)
2012/12/27 Javascript
jQuery Trim去除字符串首尾空字符的实现方法说明
2014/02/11 Javascript
Jquery通过JSON字符串创建JSON对象
2014/08/24 Javascript
jquery实现标签支持图文排列带上下箭头按钮的选项卡
2015/03/14 Javascript
javascript中innerText和innerHTML属性用法实例分析
2015/05/13 Javascript
javascript中eval解析JSON字符串
2016/02/27 Javascript
基于React.js实现原生js拖拽效果引发的思考
2016/03/30 Javascript
Javascript学习之谈谈JS的全局变量跟局部变量(推荐)
2016/08/28 Javascript
微信小程序开发探究
2016/12/27 Javascript
Javascript中return的使用与闭包详解
2017/01/11 Javascript
jQuery+Cookie实现切换皮肤功能【附源码下载】
2018/03/25 jQuery
浅谈JavaScript 代码整洁之道
2018/10/23 Javascript
用图片替换checkbox原始样式并实现同样的功能
2018/11/15 Javascript
浅入深出Vue之组件使用
2019/07/11 Javascript
javascript 对象 与 prototype 原型用法实例分析
2019/11/11 Javascript
jQuery实现动态加载瀑布流
2020/09/01 jQuery
JavaScript实现网页留言板功能
2020/11/23 Javascript
详解Python的Django框架中的templates设置
2015/05/11 Python
Python进阶之全面解读高级特性之切片
2019/02/19 Python
Python数据可视化 pyecharts实现各种统计图表过程详解
2019/08/15 Python
pytorch多进程加速及代码优化方法
2019/08/19 Python
python机器学习包mlxtend的安装和配置详解
2019/08/21 Python
Python unittest 自动识别并执行测试用例方式
2020/03/09 Python
高考考python编程是真的吗
2020/07/20 Python
python re的findall和finditer的区别详解
2020/11/15 Python
CSS3 3D位移translate效果实例介绍
2016/05/03 HTML / CSS
暑期培训随笔感言
2014/03/10 职场文书
宣传保护环境的公益广告词
2014/03/13 职场文书
房产买卖委托公证书
2014/04/04 职场文书
在校大学生才艺比赛策划书怎么写?
2019/08/26 职场文书
CSS3常见动画的实现方式
2021/04/14 HTML / CSS
MySQL COUNT函数的使用与优化
2021/05/10 MySQL
SpringCloud之@FeignClient()注解的使用方式
2021/09/25 Java/Android
SpringBoot项目部署到阿里云服务器的实现步骤
2022/06/28 Java/Android