最近升级到 python 3.3,发现原来的脚本不好使了, ConfigParser 改成 configparser 了:
修正后的例子如下:
#!/usr/bin/python
import configparser as cparser
import sys
config = cparser.ConfigParser()
ret = config.read("data.conf")
if len(ret) < 1:
#failed
sys.exit(-1)
for section in config.sections():
print ("In section [%s]" % section)
params = config.items(section)
for (k,v) in params:
print (" Key '%s' has value '%s'" % (k, v))
value = config.get('section_name', 'key_name')
print(value)
http://blog.163.com/yg_qi/blog/static/9672173520138236335511/