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?
Source code: Lib/configparser.py. This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files.
In computer science, configuration files provide the parameters and initial settings for the operating system and some computer applications. Configuration files are usually written in ASCII encoding and contain all necessary data about the specific application, computer, user or file.
The configparser module from Python's standard library defines functionality for reading and writing configuration files as used by Microsoft Windows OS. Such files usually have . INI extension. The INI file consists of sections, each led by a [section] header. Between square brackets, we can put the section's name.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With