[英]Python error : ConfigParser.NoSectionError: No section:
I'm new to Python and would like to read a config file using the ConfigParser
module. 我是Python的新手,想使用
ConfigParser
模块读取配置文件。 Here is my config file: 这是我的配置文件:
[Server]
server_name= localhost
And here is my code: 这是我的代码:
#!/usr/bin/env python
import ConfigParser
config = ConfigParser.ConfigParser()
config.read=('config.cfg')
print config.get('Server', 'server_name')
I try to run this and it gives me the error: 我尝试运行它,它给了我错误:
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'
It can't find the server section. 它找不到服务器部分。 The
config.cfg
file is in the same directory as the Python script and I have even gave the full path but still comes up with the same error. config.cfg
文件与Python脚本位于同一目录中,我什至给出了完整路径,但仍然出现相同的错误。 I have tried Python 2.6 and 2.7 on both OSX and Debian 6. What am I missing here? 我已经在OSX和Debian 6上都尝试了Python 2.6和2.7。
You have an extra character on the following line: 您在以下行上有一个额外的字符:
config.read('config.cfg')
I've removed the =
. 我删除了
=
。
With the =
, the code is valid syntactically but does something different to what's intended. 使用
=
,代码在语法上是有效的,但与预期的功能有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.