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"))
时间: 2024-11-11 04:37:28