ConfigParser-打印config.sections()返回[] [英] ConfigParser - print config.sections() returns []
本文介绍了ConfigParser-打印config.sections()返回[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 ConfigParser
模块来解析 *.ini
文件.问题是当我尝试打印 sections
或其他内容时,它返回空列表 []
.
I'm trying to use a ConfigParser
module to parse an *.ini
file. The problem is that when I try to print sections
or whatever, it returns empty list []
.
config.ini
[SERVER]
host=localhost
port=9999
max_clients=5
[REGULAR_EXPRESSIONS]
regular_expressions_file_path=commands/commands_dict
config.py
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.SafeConfigParser()
config.read("config.ini")
print config.sections()
[]
您知道问题出在哪里吗?
Do you know where is the problem?
这是我的结构的屏幕:
Here is a screen of my structure:
推荐答案
您的代码对我有用.您确定您的CWD指向其中包含正确的config.ini文件的正确目录吗?
Your code works for me. Are you sure that your CWD points to the right directory with the right config.ini file in it?
$ 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
这篇关于ConfigParser-打印config.sections()返回[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文