Python Config Parser can't find section?

12,070

Solution 1

I always use the SafeConfigParser:

from ConfigParser import SafeConfigParser

def main():
    parser = SafeConfigParser()
    parser.read('options.cfg')
    print(parser.sections())
    screen_width = parser.getint('graphics','width')
    screen_height = parser.getint('graphics','height')

Also make sure there is a file called options.cfg and specify the full path if needed, as I already commented. Parser will fail silently if there is no file found.

Solution 2

Your config file probably is not found. The parser will just produce an empty set in that case. You should wrap your code with a check for the file:

from ConfigParser import SafeConfigParser
import os

def main():
    filename = "options.cfg"
    if os.path.isfile(filename):
        parser = SafeConfigParser()
        parser.read(filename)
        print(parser.sections())
        screen_width = parser.getint('graphics','width')
        screen_height = parser.getint('graphics','height')
    else:
        print("Config file not found")

if __name__=="__main__":
    main()
Share:
12,070
user3033405
Author by

user3033405

Updated on August 30, 2022

Comments

  • user3033405
    user3033405 over 1 year

    I'm trying to use ConfigParser to read a .cfg file for my pygame game. I can't get it to function for some reason. The code looks like this:

    import ConfigParser
    def main():
        config = ConfigParser.ConfigParser()
        config.read('options.cfg')
        print config.sections()
        Screen_width = config.getint('graphics','width')
        Screen_height = config.getint('graphics','height')
    

    The main method in this file is called in the launcher for the game. I've tested that out and that works perfectly. When I run this code, I get this error:

    Traceback (most recent call last):
      File "Scripts\Launcher.py", line 71, in <module>
        Game.main()
      File "C:\Users\astro_000\Desktop\Mini-Golf\Scripts\Game.py", line 8, in main
        Screen_width = config.getint('graphics','width')
      File "c:\python27\lib\ConfigParser.py", line 359, in getint
        return self._get(section, int, option)
      File "c:\python27\lib\ConfigParser.py", line 356, in _get
        return conv(self.get(section, option))
      File "c:\python27\lib\ConfigParser.py", line 607, in get
        raise NoSectionError(section)
    ConfigParser.NoSectionError: No section: 'graphics'
    

    The thing is, there is a section 'graphics'.

    The file I'm trying to read from looks like this:

    [graphics]
    height = 600
    width = 800
    

    I have verified that it is, in fact called options.cfg. config.sections() returns only this: "[]"

    I've had this work before using this same code, but it wont work now. Any help would be greatly appreciated.

  • user3033405
    user3033405 over 9 years
    ' [] Traceback (most recent call last): File "Scripts\Launcher.py", line 71, in <module> Game.main() File "C:\Users\astro_000\Desktop\Mini-Golf\Scripts\Game.py", line 7, in main screen_width = parser.getint('graphics','width') File "c:\python27\lib\ConfigParser.py", line 359, in getint return self._get(section, int, option) File "c:\python27\lib\ConfigParser.py", line 356, in _get return conv(self.get(section, option)) File "c:\python27\lib\ConfigParser.py", line 607, in get raise NoSectionError(section) ConfigParser.NoSectionError: No section: 'graphics' '
  • tamasgal
    tamasgal over 9 years
    It works fine. Are you sure you have a file called options.cfg? Specify a full path if you're unsure about it.
  • k-nut
    k-nut over 9 years
    and in case your file is not found you should probably try feeding it the full path
  • tamasgal
    tamasgal over 9 years
    You can use a comment function instead of copying my whole code and you would have probably seen that there is already a hint to check the existence of the file.
  • user3033405
    user3033405 over 9 years
    I'm going to try to re-install python 2.7 itself
  • k-nut
    k-nut over 9 years
    @septi this actually solved the problem though. I tried copying your code to see if it helped. It did not so I wrapped it to make it actually work. I don't think a valid solution is woth a downvote...
  • tamasgal
    tamasgal over 9 years
    Maybe reinstall windows. No just a joke, why would you reinstall python? I'm absolutely sure that the path is not correct. Or the filename. Double check that! My code works absolutely fine.
  • tamasgal
    tamasgal over 9 years
    I already commented/edited my answer with the same hint before you copied my answer, so this answer is not helpful.
  • k-nut
    k-nut over 9 years
    But I actually showed how to solve this (and indeed did not see that you commented on the same matter). But well It's not worth arguing with an internet stranger. I would just like the OP to try this.
  • user3033405
    user3033405 over 9 years
    Now it returns "Config File not found". I'm going to try it with a full path.
  • user3033405
    user3033405 over 9 years
    With the full path it now works! Thank you k-nut and septi, you were very helpful. I'm going to mark this as solved now.