Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in configparser
#1
I have a configuration file (abc.ini)as below

[DEFAULTS]
plus = plus.jpg

[PATHS]
folderPath = None

import configparser
config = configparser.ConfigParser()
config.read("abc.ini", encoding="utf-8")
print(config.sections())  # works ok
config["PATHS"].folder_path = path  # path is some path 
I don't understand whats happening in config["PATHS"].folder_path = path
when I do config["PATHS"]["folder_path"] I am getting an error.

Also, to access a value, we can use config["PATHS"]["folderPath"]
but can't we use config["PATHS"].folderPath
Reply
#2
you should use the builtin: https://docs.python.org/3/library/configparser.html
The .ini file is used like a dictionary, thus the ["PATHS"]["folder_path"] access method
Reply
#3
dear Larz60+

thanks for the reply
when i use config ["PATHS"]["folder_path"] i get error
Reply
#4
you have extra [ at the end of PATHS
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Dec-21-2019, 08:26 AM)buran Wrote: you have extra [ at the end of PATHS

thanks, but that was just a typo.
Even when I fix it I am getting the error
Reply
#6
(Dec-21-2019, 08:29 AM)fullstop Wrote: Even when I fix it I am getting the error
post the full traceback in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Dec-21-2019, 08:56 AM)buran Wrote:
(Dec-21-2019, 08:29 AM)fullstop Wrote: Even when I fix it I am getting the error
post the full traceback in error tags

hi
my only problem is what is happening in config["PATHS"].folder_path = path
Reply
#8
obviously you don't want help.
Post the traceback.
Or try Larz solution, which WORKS
config["PATHS"].folder_path is invalid syntax as he explains
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
As mention can not use . access as the dictionary return has not folder_path mapped to it.
Guess get would work.
Quick test.
import configparser

config = configparser.ConfigParser()
config.read("abc.ini", encoding="utf-8")
config.sections()
>>> config["PATHS"]["folderPath"]
'None'

# Using get
>>> config["PATHS"].get('folderPath')
'None'

>>> path = 'eggs'
>>> config["PATHS"]["folderPath"] = path
>>> config["PATHS"].get('folderPath')
'eggs'

>>> # Using get can handle errors to
>>> config["PATHS"].get('foo', 'Not in config ini file')
'Not in config ini file'
Reply
#10
I have a config file (abc.ini) as below. It just have one Section with an item name= None

[Section]
name = None

Now I am calling this in my main python code as below
import configparser
config = configparser.ConfigParser()
config.read("abc.ini",encoding="utf-8")
config["Section"].new_var = "surname"
Now my problem is with last line of code. This works ok. If I check the type config["Section"].new_var, it is of class str. Now if new_var is not a method or attribute of config["Section"], how it works OK.
Also, it it not adding any item to Section. I mean if I do
print(config["Section"]["new_var"]), I will give an error
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ConfigParser(dict_type=) not behaving as expected malonn 5 3,289 Sep-08-2022, 08:42 AM
Last Post: malonn
  using alternate names in configparser Skaperen 6 2,822 Oct-01-2020, 08:27 PM
Last Post: Skaperen
Question configparser.NoSectionError (Python 3.7) alpho 2 5,777 Apr-08-2020, 10:26 PM
Last Post: micseydel
  Help with ConfigParser EricALionsFan 3 3,235 Jul-27-2018, 08:46 PM
Last Post: buran
  ConfigParser.NoSectionError: No section: 'Default' degenaro 1 14,438 Jun-08-2018, 08:33 PM
Last Post: ljmetzger
  No module named 'ConfigParser' Bani 13 56,132 May-22-2017, 08:02 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020