Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python config parser to get all the values from a section?

I want to get all the values from a section using config parser

I used this but it gives only the first value

def ConfigSectionMap(section):
  dict1 = {}
  options = Config.options(section)
  for option in options:
    try:
      dict1[option] = Config.get(section, option)
      if dict1[option] == -1:
        DebugPrint("skip: %s" % option)
    except:
      print("exception on %s!" % option)
      dict1[option] = None
    return dict1


  Config = ConfigParser.ConfigParser()
  Config.read("/etc/harvest.conf")
  print ConfigSectionMap("files").values()
like image 600
Rishabh Avatar asked Dec 20 '11 16:12

Rishabh


People also ask

What is ConfigParser ConfigParser ()?

ConfigParser is a Python class which implements a basic configuration language for Python programs. It provides a structure similar to Microsoft Windows INI files. ConfigParser allows to write Python programs which can be customized by end users easily.

How do I print a configuration file in Python?

Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.


2 Answers

Make it a dict:

dict(Config.items('Section'))
like image 145
Niclas Nilsson Avatar answered Sep 29 '22 09:09

Niclas Nilsson


You can make it a list if ordering is important

list(Config.items('Section'))
like image 35
jithu83 Avatar answered Sep 29 '22 09:09

jithu83