configparser
This module provides the ConfigParser class which implements a basic configuration language in INI files. You can use this to write Python programs which can be customized by end users easily.
Creating configuration file programatically
Section titled “Creating configuration file programatically”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 configparserconfig = 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 = 320x240color = blueIf you want to change particular field ,get the field and assign the value
settings=config['settings']settings['color']='red'Basic usage
Section titled “Basic usage”In config.ini:
[DEFAULT]debug = Truename = Testpassword = password
[FILES]path = /path/to/fileIn Python:
from ConfigParser import ConfigParserconfig = ConfigParser()
#Load configuration fileconfig.read("config.ini")
# Access the key "debug" in "DEFAULT" sectionconfig.get("DEFAULT", "debug")# Return 'True'
# Access the key "path" in "FILES" destionconfig.get("FILES", "path")# Return '/path/to/file'Syntax
Section titled “Syntax”- Each new line contains a new key value pair separated by the = sign
- Keys can be separated in sections
- In the INI file, each section title is written between brackets: []
Remarks
Section titled “Remarks”All return values from ConfigParser.ConfigParser().get are strings. It can be converted to more common types thanks to eval