Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple configuration files with Python ConfigParser

When calling ConfigParser.read you are allowed to pass a list of strings corresponding to potential locations for configuration files and the function returns a list of those files that were successfully read.

What is the default behaviour when multiple configuration files are loaded that have overlapping sections/keys? Do later files in the list override values parsed by earlier ones? Is the entire section overridden or just conflicting keys?

like image 424
crasic Avatar asked Oct 27 '10 04:10

crasic


People also ask

How do I use Configparser in Python 3?

Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.

How does Configparser work in Python?

ConfigParser is a Python class which implements a basic configuration language for Python programs. It provides a structure similar to Microsoft Windows INI files. ConfigParser allows to write Python programs which can be customized by end users easily.

How do I add config files to Python?

Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.

Does Configparser come with Python?

configparser comes from Python 3 and as such it works well with Unicode.


1 Answers

After getting around to testing it, ConfigParser overwrites the keys with each successive file, the order in which the files are read is determined by the order of the file names in the list passed to ConfigParser.read

like image 171
crasic Avatar answered Oct 04 '22 06:10

crasic