分类: Python/Ruby
2013-03-04 09:03:12
本文原始链接:
python ConfigParser读取配置文件的例子,有需要的朋友可以参考下。
1、配置文件内容
2、测试脚本
[root@xen-monitor xenscript]# cat test.py
#!/usr/bin/python -O
import ConfigParser
import string, os, sys
cf = ConfigParser.ConfigParser()
cf.read('test.conf')
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", "db_host")
db_port = cf.getint("db", "db_port")
db_user = cf.get("db", "db_user")
db_pass = cf.get("db", "db_pass")
print '*'*80
print "db_host:%s,db_pass:%s,db_user:%s,db_port:%s" % (db_host,db_pass,db_user,db_port)
3、脚本执行结果