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"))





本文转自 baiying 51CTO博客,原文链接:http://blog.51cto.com/baiying/1658537,如需转载请自行联系原作者
上一篇:Windows注册密码能保护你的数据安全吗?


下一篇:【win8技巧】应用商店里面如何搜索应用app