This article aims to read configuration files written in the common
Python3
Code #2 : Reading the file and extracting values.
Python3
Output :
Python3
Output :
Python3
Output :
Python3
.ini configuration file format. The configparser module can be used to read configuration files.
Code #1 : Configuration File
abc.ini
; Sample configuration file
[installation]
library = %(prefix)s/lib
include = %(prefix)s/include
bin = %(prefix)s/bin
prefix = /usr/local
# Setting related to debug configuration
[debug]
pid-file = /tmp/spam.pid
show_warnings = False
log_errors = true
[server]
nworkers: 32
port: 8080
root = /www/root
signature:
from configparser import ConfigParser
configur = ConfigParser()
print (configur.read('config.ini'))
print ("Sections : ", configur.sections())
print ("Installation Library : ", configur.get('installation','library'))
print ("Log Errors debugged ? : ", configur.getboolean('debug','log_errors'))
print ("Port Server : ", configur.getint('server','port'))
print ("Worker Server : ", configur.getint('server','nworkers'))
['config.ini'] Sections : ['installation', 'debug', 'server'] Installation Library : '/usr/local/lib' Log Errors debugged ? : True Port Server : 8080 Worker Server : 32One can also modify the configuration and write it back to a file using the
cfg.write() method.
Code #3 :
configur.set('server','port','9000')
configur.set('debug','log_errors','False')
import sys
configur.write(sys.stdout)
[installation] library = %(prefix)s/lib include = %(prefix)s/include bin = %(prefix)s/bin prefix = /usr/local [debug] log_errors = False show_warnings = False [server] port = 9000 nworkers = 32 pid-file = /tmp/spam.pid root = /www/root
- Configuration files are well suited to specify configuration data to your program. Within each config file, values are grouped into different sections (e.g., "installation", "debug" and "server").
- Each section then has a specific value for various variables in that section. For the same purpose, there are some prominent differences between a config file and using a Python source file.
- First, the syntax is much more permissive and “sloppy.”
configur.get('installation','PREFIX')
configur.get('installation','prefix')
'/usr/local' '/usr/local'When parsing values, methods such as getboolean() look for any reasonable value. For example, these are all equivalent.
log_errors = true log_errors = TRUE log_errors = Yes log_errors = 1The most noteworthy contrast between a config record and Python code is that, in contrast to scripts, configuration files are not executed in a top-down way. Rather, the file is read completely. On the off chance that variable substitutions are made, they are done later after the fact. For instance, it doesn't make a difference that the prefix variable is allocated after different variables that happen to utilize it.
[installation] library = %(prefix)s/lib include = %(prefix)s/include bin = %(prefix)s/bin prefix = /usr/localMultiple configuration files can be read together and their results can be merged into a single configuration using ConfigParser, which makes it so special to use. Example - A user made their own configuration file that looks as.
; ~/.config.ini [installation] prefix = /Users/beazley/test [debug] log_errors = FalseThis file can be merged with the previous configuration by reading it separately Code #4 :
import os
# Previously read configuration
print (configur.get('installation', 'prefix'))
# Merge in user-specific configuration
print (configur.read(os.path.expanduser('~/.config.ini')))
print (configur.get('installation', 'prefix'))
print (configur.get('installation', 'library'))
print (configur.getboolean('debug', 'log_errors'))
Output :
'/usr/local' ['/Users/HP/.config.ini'] '/Users/HP/test' '/Users/HP/test/lib' False