1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import ConfigParser,string,os,sys cf = ConfigParser.ConfigParser() cf.read("test.conf")
s = cf.sections() print 'section:', s o = cf.options("db") print 'options:', o v = cf.items("db") print 'db:', v print '-'*60
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("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"))
|