簡體   English   中英

Python ConfigParser.NoSectionError:沒有部分:

[英]Python ConfigParser.NoSectionError: No section:

我不斷收到 ConfigParser.NoSectionError: No section:'file' 錯誤

我的配置文件如下所示:

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

我的代碼看起來像:

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)  

錯誤是:

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

有誰知道可能導致此問題的原因是什么?

你用反斜杠定義你的路徑:

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

這些被解釋為轉義,因此沒有加載正確的文件。 (不幸的是,錯誤消息對此實例沒有多大幫助。)您需要轉義它們,或者使用原始字符串:

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

我多次遇到同樣的問題。 大多數時候,這是由於路徑錯誤。 在這種情況下,它不會顯示文件是否已找到。 它只是給出了找不到該部分的錯誤。

所以我得出的結論是,始終建議使用 os.path.join() 獲取根目錄並加入路徑

這是代碼。

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) 

和你的配置文件:

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

將文件網址更改為

 configFilePath = 'test.txt'

因為我的文件已經在我的應用程序的文件夾中

是的,您的文件路徑讀取錯誤,因為'\\'之后的每個字符都在Python中轉義。 而是這樣做:

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

或這個:

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

由於路徑長度(> 256),我收到此錯誤

它通過提供絕對路徑得到解決

 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'不正確,因此找不到該文件。
將代碼修改為類似的內容,以捕獲此類錯誤。

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