Python ConfigParser.NoSectionError: No section:

Question:

I keep getting the ConfigParser.NoSectionError: No section:’file’ error

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:Pythonconfigfiletest.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:Python27libConfigParser.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?

Asked By: c.vieira

||

Answers:

Changed the file url to

 configFilePath = 'test.txt'

as my file was already in the folder of my application

Answered By: c.vieira

You’re defining your path with backslashes:

configFilePath = 'E:Pythonconfigfiletest.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:Pythonconfigfiletest.txt'
Answered By: glibdud

configFilePath = ‘E:Pythonconfigfiletest.txt’ is incorrect so the file is not being found.
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()
Answered By: Rolf of Saxony

Yes, your file path is being read wrong because every character after an ” is escaped in Python. Instead do this:

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

or this:

configFilePath = 'E:/Python/configfile/test.txt'
Answered By: Jack Aidley

I got this error because of the path length (>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')
Answered By: Shridhar S Chini

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

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
Answered By: Arslan Arif
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.