from six.moves.configparser import ConfigParser config = ConfigParser() config.read('config.ini') # Access a configuration item database_name = config.get('database', 'name')
from six.moves.configparser import ConfigParser config = ConfigParser() config.read('config.ini') # Update a configuration item config.set('database', 'name', 'new_database_name') # Write the changes to the configuration file with open('config.ini', 'w') as f: config.write(f)
from six.moves.configparser import ConfigParser config = ConfigParser() # Add a new section to the configuration file config.add_section('database') # Set configuration items config.set('database', 'name', 'my_database') config.set('database', 'user', 'my_user') config.set('database', 'password', 'my_password') # Write the configuration file with open('config.ini', 'w') as f: config.write(f)In this example, we create a new configuration file from scratch using ConfigParser. We use the `add_section()` method to add a new section to the configuration file, and then set the configuration items using the `set()` method. Finally, we write the configuration file using the `write()` method. The six.moves.configparser module is part of the standard Python library, and is included in both Python 2 and Python 3.