【问题标题】:ConfigParser - print config.sections() returns []ConfigParser - 打印 config.sections() 返回 []
【发布时间】:2016-05-01 00:40:39
【问题描述】:

我正在尝试使用ConfigParser 模块来解析*.ini 文件。问题是当我尝试打印sections 或其他内容时,它返回空列表[]

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()

[]

你知道问题出在哪里吗?

编辑:这是我的结构的屏幕:

【问题讨论】:

  • 该代码对我有用 - 我将脚本和配置文件放在同一个目录中。当配置文件为空或没有名为config.ini 的文件时,它返回一个空列表([]),那么您确定您已保存您的编辑并且文件名正确吗?
  • 是的,它在同一个目录中。我在上面添加了一张图片。
  • 将配置文件的完整路径传递给config.read 时会发生什么,例如config.read("/tmp/config.ini")?
  • config.read("/config/config.ini") --> 没有,还是一样的空列表
  • 这个问题很荒谬。文件名中的“ini”之前有一个逗号而不是一个点...调试一小时...

标签: python config ini configparser


【解决方案1】:

我以前也遇到过同样的问题。 这是简单的解决方案: 我的“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)

你已经完成了。

【讨论】:

    【解决方案2】:

    我也有同样的问题,而且很愚蠢,问题只是我的文件结构是:

    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)
        ...
    

    这将不可避免地返回

    Exception: Reading from File: database.ini seems to return and empty object

    发现问题?

    它在我传递给filename 参数的路径字符串中!

    考虑到它应该是的结构

    "db/database.ini"

    啊。

    【讨论】:

      【解决方案3】:

      遇到了同样的问题,我不知道是什么原因造成的,但就我而言,我输入了:

      config.read = ("file-name.ini")
      

      应该是这样的

      config.read("file-name.ini")
      

      【讨论】:

        【解决方案4】:

        您的代码对我有用。您确定您的 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
        

        【讨论】:

        • 很奇怪。它对我不起作用:D 我已经添加了上面项目结构的屏幕截图。
        • 这个问题很荒谬。文件名中的“ini”之前有一个逗号而不是一个点...调试一小时...
        猜你喜欢
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 2021-02-04
        • 2016-01-08
        • 2015-11-25
        • 2017-08-21
        • 2014-10-02
        • 1970-01-01
        相关资源
        最近更新 更多