python字符串替换示例

yipeiwu_com6年前Python基础

php5.2升级到5.3后,原& new的写法已经被放弃了,可以直接new了,面对上百个php文件,手动修改简直是想要命,所以写了个脚本,分分钟搞定。

复制代码 代码如下:

#-*- coding:utf-8 -*-
#!/usr/bin/python  

import os

#定义程序根目录
rootpath='D:\\wamp\\www\\erp\\app'

def m_replace(path):
 for item in os.listdir(path):
  nowpath=os.path.join(path,item)
  if os.path.isdir(nowpath):
   m_replace(nowpath)
  else:
   if nowpath.find('.php')>0:
    f=open(nowpath,'r+')
    content=f.read().replace('& new ','new ')
    open(nowpath,'w').write(str(content))
    f.close()

if __name__=="__main__":
 m_replace(rootpath)

相关文章

python实现集中式的病毒扫描功能详解

python实现集中式的病毒扫描功能详解

本文实例讲述了python实现集中式的病毒扫描功能。分享给大家供大家参考,具体如下: 一 点睛 本次实践实现了一个集中式的病毒扫描管理,可以针对不同业务环境定制扫描策略,比如扫...

python使用turtle绘制国际象棋棋盘

python使用turtle绘制国际象棋棋盘

本文实例为大家分享了python使用turtle画国际象棋棋盘的具体代码,供大家参考,具体内容如下 使用的方法是每一个小格每一个小格的画 import turtle for i in...

python字符串常用方法

1、 isalnum() :判断字符串所有的字符都是字母或者数字。返回true和false In [1]: str1='jiangwei520' In [2]: str2='jian...

使用python turtle画高达

使用python turtle画高达

我就废话不多说了,直接上代码吧! import turtle t=turtle.Turtle() turtle.Turtle().screen.delay(0) tleft=turt...

python中的文件打开与关闭操作命令介绍

1.文件打开与关闭 在python,使用open函数,可以打开一个已经存在的文件,或者创建一个新文件 open(文件名,访问模式)。 f = open('test.txt', 'w...