ConfigParser
Python は初めてで、モジュールを使用して構成ファイルを読みたいと思っています。ここに私の設定ファイルがあります:
[Server]
server_name= localhost
そして、ここに私のコードがあります:
#!/usr/bin/env python
import ConfigParser
config = ConfigParser.ConfigParser()
config.read=('config.cfg')
print config.get('Server', 'server_name')
これを実行しようとすると、次のエラーが表示されます。
File "./read_config.py", line 10, in <module>
print config.get('Server', 'server_name')
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ConfigParser.py", line 531, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'Server'
サーバーセクションが見つかりません。ファイルはconfig.cfg
Python スクリプトと同じディレクトリにあり、完全なパスを指定しても同じエラーが発生します。OSX と Debian 6 の両方で Python 2.6 と 2.7 を試しました。