Python provides the ConfigParset
class in order to implement basic configuration management. The configuration is stored as a Microsoft Windows INI file which is a very popular format. By using ConfigParser different configurations can be created, updated, or read easily with less error and work. The configuration file can be used to store and read database connection configuration, module configuration, service configuration, etc. similar configuration files.
ConfigParser Sample Config
During this config parser tutorial, we use the following sample configuration. We have two sections named windows
and linux
with the same data like host
, user
and password
. The configuration file is named as system.ini
.
[windows]
host = windowstect.com
user = ismail
password = 123456
[linux]
host = linuxtect.com
user = ahmet
password = 654321

Read Configuration
The configparser
module provides the ConfigParser
class and it should be imported. Then a new configparser instance should be created to read, write and update the configuration file. The configuration file is named as system.ini
.
import configparser
config = configparser.ConfigParser()
config.read('system.ini')
windows_host = config['windows']['host']
windows_user = config['windows']['user']
windows_password = config['windows']['password']
linux_host = config['linux']['host']
linux_user = config['linux']['user']
linux_password = config['linux']['password']
print("Windows host:",windows_host)
print("Windows user:",windows_user)
print("Windows password:",windows_password)
print("Linux host:",linux_host)
print("Linux user:",linux_user)
print("Linux password:",linux_password)
This configreader script outputs the following text.
Windows host: windowstect.com Windows user: ismail Windows password: 123456 Linux host: linuxtect.com Linux user: ahmet Linux password: 654321
ConfigParser Sections
Some configurations are very small like in our example but some configurations can consist of a few hundreds line of configuration. The Sections
are used to put the configuration file in different sections which makes it easier to read and manage. In the sample file we have two sections named windows
and linux
where configuration data can be stored with the same name in different sections.
import configparser
config = configparser.ConfigParser()
config.read('system.ini')
sections = config.sections()
for section in sections:
print(section)
Add New Section
A new section can be added easily by using the add()
method of the sections. In the following example, we add a new section named macos
.
import configparser
config = configparser.ConfigParser()
config.read('system.ini')
sections = config.sections()
sections.append('macos')
for section in sections:
print(section)
Read Configuration from String
The configuration can be also stored as a string inside the Python code. This string configuration can be read and parsed with the configparser. The read_string()
method is used to read string configuration. And then used as a configuration file previously explained.
import configparser
config = configparser.ConfigParser()
config_string = '''
[windows]
host = windowstect.com
user = ismail
password = 123456
[linux]
host = linuxtect.com
user = ahmet
password = 654321
'''
config.read_string(config_string)
windows_host = config['windows']['host']
windows_user = config['windows']['user']
windows_password = config['windows']['password']
linux_host = config['linux']['host']
linux_user = config['linux']['user']
linux_password = config['linux']['password']
print("Windows host:",windows_host)
print("Windows user:",windows_user)
print("Windows password:",windows_password)
print("Linux host:",linux_host)
print("Linux user:",linux_user)
print("Linux password:",linux_password)
Read Configuration from Dictionary
Alternatively, the configuration can be stored inside a dictionary. The configparser can also read the dictionary type configuration with the read_dict()
method.
import configparser
config = configparser.ConfigParser()
config_dictionary = {
'windows':{ 'host':'windowstect.com','user':'ismail','password':'123456'},
'linux':{'host':'linuxtect.com','user':'ahmet','password':'654321'}
}
config.read_string(config_dictionary)
windows_host = config['windows']['host']
windows_user = config['windows']['user']
windows_password = config['windows']['password']
linux_host = config['linux']['host']
linux_user = config['linux']['user']
linux_password = config['linux']['password']
print("Windows host:",windows_host)
print("Windows user:",windows_user)
print("Windows password:",windows_password)
print("Linux host:",linux_host)
print("Linux user:",linux_user)
print("Linux password:",linux_password)
Write/Update Configuration
The existing configuration file can be updated by opening the file as file type and putting the configuration into this file with the write()
method.
import configparser
config = configparser.ConfigParser()
config.read('system.ini')
config.add_section('macos')
config['macos']['host'] = "wisetut.com"
config['macos']['user'] = "elif"
config['macos']['password'] = "123654"
with open('system.ini', 'w') as configfile:
config.write(configfile)