Pythonのスクリプトなどで、アクセスキーやメールアドレス、パスワードなどの変数をスクリプトの直接書き込まずに、設定ファイルとして分離して扱うときにConfigParserモジュールを使用します。
設定ファイルの作成
from ConfigParser import ConfigParser
config = ConfigParser()
section = 'development'
config.add_section(section)
config.set(section, 'mail', 'hogehoge@huga.com')
config.set(section, 'password', 'hogehoge')
with open('config.ini', 'w') as file:
config.write(file)
以下のような設定ファイルが作成されます。
[development] mail = hogehoge@huga.com password = hogehoge
設定ファイルの読み込み
from ConfigParser import ConfigParser
config = ConfigParser()
config.read('config.ini')
section = 'development'
print config.get(section, 'mail') # hogehoge@huga.com
print config.get(section, 'password') # hogehgoe


