python中读取配置文件操作

#-*- encoding: gb2312 -*- #哈哈我终于明白了这里加上这句话原来表示的是指定当前页面编码问题可以为中文的哦哈哈!
import ConfigParser      
import string, os, sys
cf = ConfigParser.ConfigParser()
cf.read("conf.conf")     #读取配置文件,当前是与.py在同一个目录下面的哦!
db_host = cf.get("db", "db_host")   查了API发现了这几个方法。不懂查API哈哈
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
其中的配置文件内容如下:
[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password
[concurrent]
thread=10
processor=20
以后我们可以这样来读取数据库的配置信息喽!