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 2.x如何设置命令执行的超时时间实例
Oct 19 Python
Python实现简单的用户交互方法详解
Sep 25 Python
python 实现对文件夹中的图像连续重命名方法
Oct 25 Python
Python multiprocessing多进程原理与应用示例
Feb 28 Python
python实现坦克大战游戏 附详细注释
Mar 27 Python
Python matplotlib学习笔记之坐标轴范围
Jun 28 Python
python同步windows和linux文件
Aug 29 Python
Pycharm中Python环境配置常见问题解析
Jan 16 Python
浅析Python __name__ 是什么
Jul 07 Python
Python SMTP发送电子邮件的示例
Sep 23 Python
用sleep间隔进行python反爬虫的实例讲解
Nov 30 Python
完美解决Pycharm中matplotlib画图中文乱码问题
Jan 11 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的preg_match匹配字符串长度问题解决方法
2014/05/03 PHP
php实现网站顶踩功能的完整前端代码
2015/07/19 PHP
PHP入门教程之图像处理技巧分析
2016/09/11 PHP
Laravel学习笔记之Artisan命令生成自定义模板的方法
2018/11/22 PHP
thinkPHP和onethink微信支付插件分享
2019/08/11 PHP
JavaScript DOM学习第六章 表单实例
2010/02/19 Javascript
Javascript异步编程的4种方法让你写出更出色的程序
2013/01/17 Javascript
在父页面调用子页面的JS方法
2013/09/29 Javascript
JavaScript数组Array对象增加和删除元素方法总结
2015/01/20 Javascript
jQuery焦点控制图层展示延迟隐藏的方法
2015/03/09 Javascript
javascript中eval和with用法实例总结
2015/11/30 Javascript
浅谈JS中json数据的处理
2016/06/30 Javascript
js实现随机抽选效果、随机抽选红色球效果
2017/01/13 Javascript
vue+element表格导出为Excel文件
2019/09/26 Javascript
js实现数据导出为EXCEL(支持大量数据导出)
2020/03/31 Javascript
toString.call()通用的判断数据类型方法示例
2020/08/28 Javascript
在vue中实现某一些路由页面隐藏导航栏的功能操作
2020/09/21 Javascript
[03:02]2020完美世界城市挑战赛(秋季赛)总决赛回顾
2021/03/11 DOTA
浅谈Python程序与C++程序的联合使用
2015/04/07 Python
python识别图像并提取文字的实现方法
2019/06/28 Python
django表单的Widgets使用详解
2019/07/22 Python
python 使用递归回溯完美解决八皇后的问题
2020/02/26 Python
MATLAB数学建模之画图汇总
2020/07/16 Python
html5中监听canvas内部元素点击事件的三种方法
2019/04/28 HTML / CSS
html5-websocket基于远程方法调用的数据交互实现
2012/12/04 HTML / CSS
HTML5拖拉上传文件的简单实例
2017/01/11 HTML / CSS
中国电子产品批发商/跨境电商/外贸网:Sunsky-online
2020/04/20 全球购物
会计专业毕业生求职信分享
2014/01/03 职场文书
《珍珠泉》教学反思
2014/02/20 职场文书
大队委竞选演讲稿
2014/04/28 职场文书
工地宣传标语
2014/06/18 职场文书
会计实训报告范文
2014/11/04 职场文书
装饰技术负责人岗位职责
2015/04/13 职场文书
2016年习主席讲话学习心得体会
2016/01/20 职场文书
2019数学教师下学期工作总结
2019/06/27 职场文书
java泛型通配符详解
2021/07/25 Java/Android