ConfigParser - 打印 config.sections() returns []
ConfigParser - print config.sections() returns []
我正在尝试使用 ConfigParser
模块来解析 *.ini
文件。问题是,当我尝试打印 sections
或其他内容时,它 returns 空列表 []
.
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()
[]
你知道问题出在哪里吗?
编辑:这是我的结构的屏幕:
你的代码对我有用。您确定您的 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
遇到了同样的问题,我不知道是什么原因造成的,但就我而言,我输入了:
config.read = ("file-name.ini")
应该是
config.read("file-name.ini")
我遇到了同样的问题,而且很愚蠢,问题很简单,我的文件结构是:
src_folder
|db
|database.ini
|config.py
在 config.py
中我有一个这样的函数:
#!/usr/bin/python
from configparser import ConfigParser
def config(filename="database.ini", section="postgresql") :
# create a parser
parser = ConfigParser()
if not parser.read(filename):
raise Exception(f"Reading from File: {filename} seems to return and empty object")
else:
parser.read(filename)
...
这将不可避免地 return
Exception: Reading from File: database.ini seems to return and empty object
发现问题了吗?
它在我传递给 filename
参数的路径字符串中!
给定的结构应该是
"db/database.ini"
唉。
我以前遇到过同样的问题。
这是简单的解决方案:
我的 'conf.ini' 文件。
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
现在阅读 'demo.py' 中的 'conf.int' 文件。
import configparser
import os
path = os.path.dirname(os.path.realpath(__file__))
configdir = '/'.join([path,'conf.ini'])
config = configparser.ConfigParser()
config.read(configdir)
string = config['bitbucket.org']['User']
print(string)
大功告成。
我正在尝试使用 ConfigParser
模块来解析 *.ini
文件。问题是,当我尝试打印 sections
或其他内容时,它 returns 空列表 []
.
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()
[]
你知道问题出在哪里吗?
编辑:这是我的结构的屏幕:
你的代码对我有用。您确定您的 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
遇到了同样的问题,我不知道是什么原因造成的,但就我而言,我输入了:
config.read = ("file-name.ini")
应该是
config.read("file-name.ini")
我遇到了同样的问题,而且很愚蠢,问题很简单,我的文件结构是:
src_folder
|db
|database.ini
|config.py
在 config.py
中我有一个这样的函数:
#!/usr/bin/python
from configparser import ConfigParser
def config(filename="database.ini", section="postgresql") :
# create a parser
parser = ConfigParser()
if not parser.read(filename):
raise Exception(f"Reading from File: {filename} seems to return and empty object")
else:
parser.read(filename)
...
这将不可避免地 return
Exception: Reading from File: database.ini seems to return and empty object
发现问题了吗?
它在我传递给 filename
参数的路径字符串中!
给定的结构应该是
"db/database.ini"
唉。
我以前遇到过同样的问题。 这是简单的解决方案: 我的 'conf.ini' 文件。
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
现在阅读 'demo.py' 中的 'conf.int' 文件。
import configparser
import os
path = os.path.dirname(os.path.realpath(__file__))
configdir = '/'.join([path,'conf.ini'])
config = configparser.ConfigParser()
config.read(configdir)
string = config['bitbucket.org']['User']
print(string)
大功告成。