现在的位置: 首页 > 综合 > 正文

python替换文件中的配置值

2013年01月12日 ⁄ 综合 ⁄ 共 484字 ⁄ 字号 评论关闭
##将文本中的name=配置项值更新为name=newvalue
import re
import os
p=re.compile(r'(^name)=(.+)')
f=file('d://soft/list.txt','r')
f2=file('d://soft/list3.txt','w')
while True:
	line=f.readline()
	if len(line)==0:
		break
	line=p.sub(r'\1=newvalue',line)
	#line=p.sub(r'\1=newfff',f.readline())
	print line
	f2.write(line)
f2.close()
f.close()
if os.path.exists('d://soft/list.txt.bak2'):
	os.remove('d://soft/list.txt.bak2');
os.rename('d://soft/list.txt', 'd://soft/list.txt.bak2')
os.rename('d://soft/list3.txt', 'd://soft/list.txt')

抱歉!评论已关闭.