I have a config file as follows:
[job]
mailto=bob
logFile=blahDeBlah.txt
I want to read the options using SafeConfigParser
:
values = {}
config = ConfigParser.SafeConfigParser()
try:
config.read(configFile)
jobSection = 'job'
values['mailto'] = config.get( jobSection, 'mailto' )
values['logFile'] = config.get( jobSection, 'logFile' )
# it is not there
values['nothingThere'] = config.get( jobSection, 'nothingThere' )
.... # rest of code
The last line of course will throw an error. How can I specify a default value for the config.get()
method?
Then again, if I have an options file as follows:
[job1]
mailto=bob
logFile=blahDeBlah.txt
[job2]
mailto=bob
logFile=blahDeBlah.txt
There seems to be no way to specify default options for job1
different from the default options in section job2
.
Use the defaults
parameter to the constructor:
# class ConfigParser.SafeConfigParser([defaults[, dict_type]])
#
config = ConfigParser.SafeConfigParser({'nothingThere': 'lalalalala'})
...
...
# If the job section has no "nothingThere", "lalalalala" will be returned
#
config.get(jobSection, 'nothingThere')