Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ConfigParser.NoSectionError: No section:

Tags:

python

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:\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?

like image 279
c.vieira Avatar asked Jan 26 '16 14:01

c.vieira


3 Answers

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'
like image 58
glibdud Avatar answered Nov 14 '22 21:11

glibdud


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')
like image 24
Shridhar S Chini Avatar answered Nov 14 '22 21:11

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
like image 3
Arslan Arif Avatar answered Nov 14 '22 21:11

Arslan Arif