##将文本中的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')