Python Language configparser Creating configuration file programatically

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Configuration file contains sections, each section contains keys and values. configparser module can be used to read and write config files. Creating the configuration file:-

import configparser
config = configparser.ConfigParser()
config['settings']={'resolution':'320x240',
                    'color':'blue'}
with open('example.ini', 'w') as configfile:
    config.write(configfile)

The output file contains below structure

[settings]
resolution = 320x240
color = blue

If you want to change particular field ,get the field and assign the value

settings=config['settings']
settings['color']='red'


Got any Python Language Question?