好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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字符串替换示例的详细内容...

  阅读:35次