Python with Configuration File.
Reading configuration or properties file is a basic ingredient that is used in most of the scripting and full feature language. There are in-build library and other 3rd party library to read this type of type with ease. We can even use simple file reader but the ease of using such library makes the properties or configuration file awesome.
In python if you ever encounter to read such key/value type file, ConfigParser is the one that you should use. The Configparser module
contains most the needs that you would have to use a configuration type files and won’t need to implement your own parser.
First lets discuss what is what is configuration file type. If you are familiar you can skip this part.
Configuration File Type
Configuration file are those that have key/value type data. The separator would be either “:” or “=” with key in left side and value at right side. Comments are also allowed in such type with “#” or “;”. Leading white space are remove from the value. It also contain section part with “[this_is_section_header]” to differentiate and scope the keys. The file type can be of anything but mostly used are : cgf, ini, properties etc.
Example:
It also support interpolation
which means you can specify to refer other key(option) using “%option” but this can be parse using Configparser.SafeConfigParser().
Reading Configuration file
First import the require module.
This is import SafeConfigParser to parse the configuration type file and the other two are the exception type that is used frequently.
NoSectionError :
Exception type raise when there is no section that is specified is found.
NoOptionError :
Exception type raise when there is no option that is specified is found.
The following will read the configuration file and return the SafeConfigParser instance that read the file.
Now you can define a function that extract the require information. This way you don’t have to read the file multiple time and use the instance.
You can even read all the keys/values in a section via
Wrting to Configuration File
To write to Configuration file:
Reading configuration file without Section
Java property file with extension properties or any configuration that does not have section would be problem to read as the ConfigParser API need section to get the key/value.
The solution is to create a fake section by initializing from a class and read the file. This way ConfigParser will see them with a face section.