Python 操作配置文件

import ConfigParser

cf = ConfigParser.ConfigParser()
cf.read("config.ini")


s = cf.sections()
#print 'section:', s

o = cf.options("db")
#print 'options:', o


v = cf.items("db")
#print 'db:', v

db_host = cf.get("db", "db_host")
db_port = cf.get("db", "db_port")
db_user = cf.get("db", "db_user")
db_pass = cf.get("db", "db_pass")

#print "db_host:", db_host
#print "db_port:", db_port
#print "db_user:", db_user
#print "db_pass:", db_pass

#cf.set("db", "db_pass", "zhang3")
#cf.write(open("test.conf", "w"))

 

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("test.conf", "w"))


cf.remove_option('liuqing', 'int')
cf.remove_section('liuqing')
cf.write(open("test.conf", "w"))

 

你可能感兴趣的:(Python 操作配置文件)