Search code examples
pythonconfigparser

When reading multiple config files, ConfigParser overwrites previous files, why?


So I wanted to start by saying I have been looking through out SO for an answer to this, and haven't been able to find anything useful. I've also looked through Python's docs and failed to find something useful. My last question was slightly lazy and received negative feedback, so I'm doing everything I can to ask a simple and straightforward question here. As always, thanks in advance for any help!

So, can someone please explain this odd behavior that I am experiencing with Python's ConfigParser. I have two different configuration files, each with a Section 1. The two files have differing numbers of options, but the one with a lesser number of options is overwritten. Here is the code and the output:

First Config File: test1.ini

[Section 1]
Option 1 : One
Option 2 : Two
Option 3 : None
Option 4 : Four

Second Config File: test2.ini

[Section 1]
Option 1 : One
Option 2 : None
Option 3 : Three

Driver that reads config files

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()

def ParseThis(file, section):
    parser.read(file)

    for option in parser.options(section):
        print "\t" + option
        try:
            if parser.get(section, option) != 'None':
                print option + ": " + parser.get(section, option)
            else:
                print option + ": Option doesn't exist"
        except:
            print option + ": Something went wrong"

print "First File:"
print "Section 1"
ParseThis('test2.ini', 'Section 1')


print "\n"
print "Second File: "
print "Section 1"
ParseThis('test1.ini', 'Section 1')

print "\n"
print "First File: "
print "Section 1"
ParseThis('test2.ini', 'Section 1')

And Here is the output that I get, which makes no sense.

First File:
Section 1
    option 1
option 1: One
    option 2
option 2: Option doesn't exist
    option 3
option 3: Three


Second File: 
Section 1
    option 1
option 1: One
    option 2
option 2: Two
    option 3
option 3: Option doesn't exist
    option 4
option 4: Four


First File: 
Section 1
    option 1
option 1: One
    option 2
option 2: Option doesn't exist
    option 3
option 3: Three
    option 4   <== why this line?
option 4: Four <== why this line?

Solution

  • A single ConfigParser instance is meant to represent a single configuration, which may be derived from multiple files in a "precedence order" such that later files override earlier ones. The documentation does not make this totally clear, but does say:

    This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user’s home directory, and some system-wide directory), and all existing configuration files in the list will be read.

    If you want the configurations you read to be kept separate, you need to create a separate SafeConfigParser instance for each one. Move your parser = SafeConfigParser() line inside the function.