39

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?

Holloway
  • 6,412
  • 1
  • 26
  • 33
c.vieira
  • 413
  • 1
  • 4
  • 7

6 Answers6

31

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'
glibdud
  • 7,550
  • 4
  • 27
  • 37
  • Hi thanks glibdud figured this out just before you posted, :) but thanks for the answer it was correct :D and would of solved my issue if I hadn't managed to figure it out just before – c.vieira Jan 26 '16 at 15:13
13

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

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Arslan Arif
  • 325
  • 2
  • 12
  • `type object 'ConfigParser' has no attribute 'NoOptionError'` – john k Dec 22 '21 at 18:40
  • Hi @johnktejik, You can replace it with a more detailed exceptions according to your requirements or if you just want it get running then replace it with following generic exception like ==> except Exception as e: – Arslan Arif Dec 23 '21 at 13:17
2

Changed the file url to

 configFilePath = 'test.txt'

as my file was already in the folder of my application

c.vieira
  • 413
  • 1
  • 4
  • 7
2

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'
Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
0

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.

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()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60