事先道歉,因为我确信这是一个非常基本的错误,但我找不到答案.我对Python很新,并希望使用ConfigParser模块读取配置文件.这是我的配置文件:
[Server]
server_name= localhost
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#!/usr/bin/env python
import ConfigParser
config = ConfigParser.ConfigParser()
config.read=('config.cfg')
print config.get('Server', 'server_name')
Run Code Online (Sandbox Code Playgroud)
我尝试运行它,它给了我错误:
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'
Run Code Online (Sandbox Code Playgroud)
它找不到服务器部分.config.cfg文件与Python脚本位于同一目录中,我甚至提供了完整路径,但仍然出现相同的错误.我已经在OSX和Debian 6上尝试过Python 2.6和2.7.我很尴尬地说我很难过.我在这里失踪了什么?