2
提前道歉,我相信這是一個非常基本的錯誤,但我無法找到答案。我對Python很新,想用ConfigParser模塊讀取一個配置文件。下面是我的配置文件:Python錯誤:ConfigParser.NoSectionError:沒有部分:
[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。我很尷尬地說我很難過。我在這裏錯過了什麼?