So I wanted to start by saying that I was looking for SO to answer this question and could not find anything useful. I also looked through Python docs and couldn't find anything useful. My last question was slightly lazy and received a negative review, so I do my best to ask a simple and clear question. As always, in advance for your help!
So, can someone explain this strange behavior that I am experiencing with Python ConfigParser. I have two different configuration files, each of which has Section 1. These two files have a different number of options, but one with fewer parameters is overwritten. Here is the code and output:
First configuration file: test1.ini
[Section 1]
Option 1 : One
Option 2 : Two
Option 3 : None
Option 4 : Four
Second configuration file: test2.ini
[Section 1]
Option 1 : One
Option 2 : None
Option 3 : Three
A driver that reads configuration 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 result that I get that does not make 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
source
share