64

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()
sk8forether
  • 247
  • 2
  • 9
Rishabh
  • 3,752
  • 4
  • 47
  • 74
  • 5
    Your `return` is not properly indented and your function returns in the first iteration of the for loop. Remove two spaces. – Chewie Dec 20 '11 at 16:27

2 Answers2

148

Make it a dict:

dict(Config.items('Section'))
Niclas Nilsson
  • 5,691
  • 3
  • 30
  • 43
15

You can make it a list if ordering is important

list(Config.items('Section'))
jithu83
  • 539
  • 6
  • 11
  • Does not work for me (the version with dict does). I get this: *** Error in argument: "(Config.items('Section'))". – Jan Pisl May 11 '20 at 22:01