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
.
A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.
Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.
An INI file is a configuration file for computer software that consists of a text-based content with a structure and syntax comprising key–value pairs for properties, and sections that organize the properties.
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')
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