Change a value with configparser for python

configparser is a great module to store configuration information into a file
https://docs.python.org/2/library/configparser.html
a config file could look like this:

[DEFAULT]
value = 7

To load the value from the file

import configparser

config = configparser.ConfigParser()
config.read('config.ini')
value = self.config['DEFAULT']['value']
print(value)
# output value 7

To change the value in the file:

# set new value
config.set('DEFAULT', 'value', '42')

# save the file
with open(self.config_filename, 'w') as configfile:
    config.write('config.ini')

Tagged with: