Heim >Backend-Entwicklung >Python-Tutorial >python字符串替换示例

python字符串替换示例

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

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)

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn