python 中使用ConfigParser类修改配置文件

配置文件的格式:

  • [user]  
  • user_ip=127.0.0.1  
  • user_name=testuser
  • user_id=13

import ConfigParser

conf = ConfigParser.ConfigParser()

filepath = "c:\test.conf"

conf.read(filepath)

node ="user"

key = "user_id"

value = "45"

conf.set(node,key,value)
fh = open(filepath ,'w')

conf.write(fh)#把要修改的节点的内容写到文件中

fh.close()

posted @ 2016-06-23 13:16  巫谢  阅读(1959)  评论(0编辑  收藏  举报