# getfloat() raises an exception if the value is not a float # getint() and getboolean() also do this for their respective types float = config.getfloat('Section1', 'float') int = config.getint('Section1', 'int') printfloat + int
# Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'. # This is because we are using a RawConfigParser(). if config.getboolean('Section1', 'bool'): print config.get('Section1', 'foo')
# Set the third, optional argument of get to 1 if you wish to use raw mode. print config.get('Section1', 'foo', 0) # -> "Python is fun!" print config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!"
# The optional fourth argument is a dict with members that will take # precedence in interpolation. print config.get('Section1', 'foo', 0, {'bar': 'Documentation', 'baz': 'evil'})
1 2 3 4 5 6 7 8 9 10
import ConfigParser
# New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'}) config.read('example.cfg')
print config.get('Section1', 'foo') # -> "Python is fun!" config.remove_option('Section1', 'bar') config.remove_option('Section1', 'baz') print config.get('Section1', 'foo') # -> "Life is hard!"