简体   繁体   English

Python ConfigParser.NoSectionError:没有部分:

[英]Python ConfigParser.NoSectionError: No section:

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?有谁知道可能导致此问题的原因是什么?

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'

I have faced same issue many time.我多次遇到同样的问题。 Most of the times, it is due to the path error.大多数时候,这是由于路径错误。 In this case it does not show whether the file was found or not.在这种情况下,它不会显示文件是否已找到。 It simply gives the error that the section was not found.它只是给出了找不到该部分的错误。

So I came to the conclusion that it is always recommended to get root directory and join path using os.path.join()所以我得出的结论是,始终建议使用 os.path.join() 获取根目录并加入路径

Here is the code.这是代码。

import os
from pathlib import Path
import configparser

path = Path(__file__)
ROOT_DIR = path.parent.absolute()
config_path = os.path.join(ROOT_DIR, "config.ini")

config = configparser.ConfigParser()
config.read(config_path)

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) 

And your config file:和你的配置文件:

[file]
api_access_id = 123445567
api_default_org = NAME
api_secret_key = kjvhkd28unfkjs
api_base_url = www.fakeurl.com/api

Changed the file url to 将文件网址更改为

 configFilePath = 'test.txt'

as my file was already in the folder of my application 因为我的文件已经在我的应用程序的文件夹中

Yes, your file path is being read wrong because every character after an '\\' is escaped in Python. 是的,您的文件路径读取错误,因为'\\'之后的每个字符都在Python中转义。 Instead do this: 而是这样做:

configFilePath = 'E:\\Python\\configfile\\test.txt'

or this: 或这个:

configFilePath = 'E:/Python/configfile/test.txt'

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')

configFilePath = 'E:\\Python\\configfile\\test.txt' is incorrect so the file is not being found. configFilePath ='E:\\ Python \\ configfile \\ test.txt'不正确,因此找不到该文件。
Amend your code to something like this, to trap such an error. 将代码修改为类似的内容,以捕获此类错误。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM