我正在尝试使用ConfigParser
模块来解析*.ini
文件。问题是当我尝试打印时sections
或者其他什么,它返回空列表[]
.
配置文件
[SERVER]
host=localhost
port=9999
max_clients=5
[REGULAR_EXPRESSIONS]
regular_expressions_file_path=commands/commands_dict
配置文件
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.SafeConfigParser()
config.read("config.ini")
print config.sections()
[]
你知道问题出在哪里吗?
EDIT: Here is a screen of my structure:
你的代码对我有用。您确定您的 CWD 指向正确的目录,其中包含正确的 config.ini 文件吗?
$ cat config.ini
[SERVER]
host=localhost
port=9999
max_clients=5
[REGULAR_EXPRESSIONS]
regular_expressions_file_path=commands/commands_dict
$ python2.7
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ConfigParser
>>> cp = ConfigParser.SafeConfigParser()
>>> cp.read('config.ini')
['config.ini']
>>> cp.sections()
['SERVER', 'REGULAR_EXPRESSIONS']
>>> ^D
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)