#!/usr/bin/python
# -*- coding: utf-8 -*-
import ConfigParser
cf = ConfigParser.ConfigParser()
cf.read("cfg.txt")
# 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:
s = cf.sections()
print 'section:', s
# 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
o = cf.options("db")
print 'options:', o
# 获取指定section 的配置信息。
v = cf.items("db")
print 'db:', v
# 可以按照类型读取出来
db_host = cf.get("db", "db_host")
db_port = cf.getint("db", "db_port")
db_user = cf.get("db", "db_user")
db_pass = cf.get("db", "db_pass")
# 返回的是整型的
threads = cf.getint("concurrent", "thread")
processors = cf.getint("concurrent", "processor")
print "db_host:", db_host
print "db_port:", db_port
print "db_user:", db_user
print "db_pass:", db_pass
print "thread:", threads
print "processor:", processors
cf.set("db", "db_pass", "zhaowei")
cf.write(open("cfg.txt", "w"))
# 添加一个section。(同样要写回)
'''
cf.add_section('liuqing')
cf.set('liuqing', 'int', '15')
cf.set('liuqing', 'bool', 'true')
cf.set('liuqing', 'float', '3.1415')
cf.set('liuqing', 'baz', 'fun')
cf.set('liuqing', 'bar', 'Python')
cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!')
cf.write(open("cfg.txt", "w"))
'''
# 移除section 或者option 。(只要进行了修改就要写回的哦)
# cf.remove_option('liuqing','int')
cf.remove_section('liuqing')
cf.write(open("cfg.txt", "w"))