Python ConfigParser.NoSectionError:无部分:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35016479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python ConfigParser.NoSectionError: No section:
提问by c.vieira
I keep getting the ConfigParser.NoSectionError: No section:'file' error
我不断收到 ConfigParser.NoSectionError: No section:'file' 错误
My config file looks like:
我的配置文件看起来像:
[file]
api_access_id = 123445567
api_default_org = NAME
api_secret_key = kjvhkd28unfkjs
api_base_url = www.fakeurl.com/api
My code looks like:
我的代码看起来像:
config = ConfigParser.RawConfigParser()
configFilePath = 'E:\Python\configfile\test.txt'
config.read(configFilePath)
try:
api_access_id = config.get('file', 'api_access_id')
api_secret_key = config.get('file', 'api_secret_key')
api_default_org = config.get('file', 'api_default_org')
api_base_url = config.get('file', 'api_base_url')
except ConfigParser.NoOptionError :
print('could not read configuration file')
sys.exit(1)
The error is:
错误是:
Traceback (most recent call last):
File "E:/Python/Testapi.py", line 13, in <module>
api_access_id = config.get('file', 'api_access_id')
File "C:\Python27\lib\ConfigParser.py", line 330, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'file'
Process finished with exit code 1
Does anyone know what could be causing this issue?
有谁知道是什么导致了这个问题?
采纳答案by glibdud
You're defining your path with backslashes:
你用反斜杠定义你的路径:
configFilePath = 'E:\Python\configfile\test.txt'
These get interpreted as escapes, and as a result the correct file isn't being loaded. (Unfortunately, the error message doesn't help much in this instance.) You either need to escape them, or use a raw string:
这些被解释为转义,因此没有加载正确的文件。(不幸的是,错误消息在这种情况下没有多大帮助。)您要么需要转义它们,要么使用原始字符串:
configFilePath = r'E:\Python\configfile\test.txt'
回答by c.vieira
Changed the file url to
将文件 url 更改为
configFilePath = 'test.txt'
as my file was already in the folder of my application
因为我的文件已经在我的应用程序的文件夹中
回答by Rolf of Saxony
configFilePath = 'E:\Python\configfile\test.txt' is incorrect so the file is not being found.
Amend your code to something like this, to trap such an error.
configFilePath = 'E:\Python\configfile\test.txt' 不正确,因此找不到该文件。
将您的代码修改为这样的内容,以捕获此类错误。
import ConfigParser, sys
config = ConfigParser.RawConfigParser()
configFilePath = './config.cfg'
try:
config.read(configFilePath)
except Exception as e :
print(str(e))
try:
api_access_id = config.get('file', 'api_access_id')
api_secret_key = config.get('file', 'api_secret_key')
api_default_org = config.get('file', 'api_default_org')
api_base_url = config.get('file', 'api_base_url')
except Exception as e :
print(str(e),' could not read configuration file')
print api_access_id, api_secret_key, api_default_org, api_base_url
sys.exit()
回答by Hyman Aidley
Yes, your file path is being read wrong because every character after an '\' is escaped in Python. Instead do this:
是的,您的文件路径被读取错误,因为在 Python 中 '\' 之后的每个字符都被转义了。而是这样做:
configFilePath = 'E:\Python\configfile\test.txt'
or this:
或这个:
configFilePath = 'E:/Python/configfile/test.txt'
回答by Shridhar S Chini
I got this error because of the path length (>256)
由于路径长度(> 256),我收到此错误
it got resolved by giving absolute path
它通过提供绝对路径解决了
import os
thisfolder = os.path.dirname(os.path.abspath(__file__))
initfile = os.path.join(thisfolder, 'you_file_name.init')
# print thisfolder
config = RawConfigParser()
res = config.read(initfile)
data = config.get('section_name', 'variable_name')