I'm using the RawConfigParser
to read and update an ini style configuration file. This works all without problems in the standard configuration.
My problem is that I'm reading different config files in the same script. Without going too much into detail, when I read the second config file its values are merged with the first ini file.
Original contents of both ini files
infile_1.ini
[section]
option = Original value
extraoption = not in ini file 2
inifile_2.ini
[section]
option = Original value
This is the code I use to alter the contents of the ini file. It's a short piece of code to reproduce the problem
import ConfigParser
class status_file:
myConfig = ConfigParser.RawConfigParser()
myConfig.optionxform = str
def __init__(self, str_statusfile):
self.myConfig.read( str_statusfile)
self.statusfile = str_statusfile
def option(self, new_value):
self.myConfig.set("section","option",new_value)
with open( self.statusfile, "w") as ini_out:
self.myConfig.write( ini_out )
statusfiles = ['inifile_1.ini', 'inifile_2.ini']
for myStatus in statusfiles:
myConfig = status_file(myStatus)
myConfig.option("Something new")
After this code is executed the content of the ini files has changed as wished. But in the second ini file, options from the first ini file are merged.
infile_1.ini
[section]
option = Something New
extraoption = not in ini file 2
inifile_2.ini
[section]
option = Something New
extraoption = not in ini file 2
Is there some way I can reset the ConfigParser
content or some other solution?
I guess it's related to construction and deconstruction of the ConfigParser
object?
I tried it without the class and then there is no problem.
I'm a beginner in Python so this may not be the best way of handling this.
The real application is dynamic in the sense that the number of ini files and the location change during use of the application. The list of ini files is built with os.listdir(folder)