from six.moves.configparser import ConfigParser config = ConfigParser() config.read('myconfig.ini') config.set('Section1', 'key1', 'new_value') with open('myconfig.ini', 'w') as configfile: config.write(configfile)
from six.moves.configparser import ConfigParser config = ConfigParser() config['Section1'] = {'key1': 'value1'} with open('myconfig.ini', 'w') as configfile: config.write(configfile)In this example, we create a new configuration file from scratch by defining a new key-value pair for `Section1`. We then save the new configuration file to disk. The `six` package provides compatibility tools for working with both Python 2 and 3. The `six.moves` subpackage includes imports that will work in both versions of Python, including the `configparser` module used in these examples.