gpt4 book ai didi

Python错误: ConfigParser. NoSectionError:没有节:

转载 作者:行者123 更新时间:2023-11-28 22:03:25 25 4
gpt4 key购买 nike

我是 Python 新手,想使用 ConfigParser 模块读取配置文件。这是我的配置文件:

[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。我在这里错过了什么?

最佳答案

你在下一行有一个额外的字符:

config.read('config.cfg')

我已经删除了 =

使用 =,代码在语法上是有效的,但做了一些与预期不同的事情。

关于Python错误: ConfigParser. NoSectionError:没有节:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9227587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com