python模块ConfigParser操作配置文件
python模块ConfigParser 操作ini格式文件
cat test.txt
[host]
web01 = 10.10.10.10
web02 = 20.20.20.20
[db]
mysql01 = 1.1.1.1
mysql02 = 2.2.2.2
#!/usr/bin/env python
#coding:utf8
import ConfigParser,string,os,sys
cf = ConfigParser.ConfigParser()
cf.read("test.txt")
s = cf.sections()
print ‘sections:‘,s
o = cf.options(‘db‘)
print ‘options:‘,o
v = cf.items(‘db‘)
print ‘db:‘,v
print ‘-‘ * 60
db_host = cf.get("db","mysql02")
ip = cf.get("host","web02")
print "db_host:",db_host
print "ip:",ip
cf.set("db","mysql01","100.100.100.100")
cf.write(open("test.txt","w"))
cf.add_section("zabbix")
cf.set("zabbix","monitor01","10.17.4.4")
cf.write(open("test.txt","w"))
cf.remove_option("db","mysql02")
cf.remove_section("host")
cf.write(open("test.txt","w"))
本文出自 “赵海华_运维之路” 博客,请务必保留此出处http://baiying.blog.51cto.com/1068039/1658537
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。