Booleans in ConfigParser always return True

Use getboolean():

print config.getboolean('main', 'some_boolean') 
print config.getboolean('main', 'some_other_boolean')

From the Python manual:

RawConfigParser.getboolean(section, option)

A convenience method which coerces the option in the specified section to a Boolean value. Note that the accepted values for the option are “1”, “yes”, “true”, and “on”, which cause this method to return True, and “0”, “no”, “false”, and “off”, which cause it to return False. These string values are checked in a case-insensitive manner. Any other value will cause it to raise ValueError.

Such as:

my_bool = config.getboolean('SECTION','IDENTIFIER')

The bool() constructor converts an empty string to False. Non-empty strings are True. bool() doesn’t do anything special for “false”, “no”, etc.

>>> bool('false')
True
>>> bool('no')
True
>>> bool('0')
True
>>> bool('')
False

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)