PythonでConfigParserモジュールを操作する

Pythonコード:
#-*- encoding: utf-8 -*-
import ConfigParser
import string, os, sys

cf = ConfigParser.ConfigParser()
cf.read(“startnews_testconfig.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", “StarNews24")
cf.write(open(“startnews_testconfig.conf", “w"))

Development

Posted by arkgame