Home >Backend Development >Python Tutorial >python字符串替换示例

python字符串替换示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 11:30:051372browse

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)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn