This article aims to read configuration files written in the common
.ini configuration file format. The configparser module can be used to read configuration files.
Code #1 : Configuration File
Code #2 : Reading the file and extracting values.
['config.ini'] Sections : ['installation', 'debug', 'server'] Installation Library : '/usr/local/lib' Log Errors debugged ? : True Port Server : 8080 Worker Server : 32
One can also modify the configuration and write it back to a file using the
Code #3 :
[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.”
Names used in a config file are also assumed to be case-insensitive as shown in the code below –
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 = 1
The 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/local
Multiple 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 = False
This file can be merged with the previous configuration by reading it separately
Code #4 :
'/usr/local' ['/Users/HP/.config.ini'] '/Users/HP/test' '/Users/HP/test/lib' False
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Reading and Writing to text files in Python
- Reading CSV files in Python
- Reading and Writing XML Files in Python
- Reading and Writing CSV Files in Python
- Python | Django-allauth setup and Configuration
- Python - API.configuration() in Tweepy
- PYGLET – Getting Window Configuration
- Reading Python File-Like Objects from C | Python
- Reading and Generating QR codes in Python using QRtools
- Reading an image in OpenCV using Python
- Reading images in Python
- Python - Reading last N lines of a file
- Python | Reading contents of PDF using OCR (Optical Character Recognition)
- Reading and Writing JSON to a File in Python
- Reading Images With Python - Tkinter
- Reading an excel file using Python openpyxl module
- Reading an excel file using Python
- Downloading files from web using Python
- Working with csv files in Python
- Working with PDF files in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.