![]() |
VOOZH | about |
A configuration file is a plain text file used to store application settings and parameters in a readable format.
Store and manage application settings in Python using configuration files to improve flexibility, readability, and maintainability. Below are some examples that demonstrate how to write and use a configuration file in Python:
The configparser module is included in Python’s standard library, so no additional installation is required. It can be imported directly into any Python script or application.
In this example, a ConfigParser object is created with two sections: General and Database. Each section contains key–value pairs, and the configuration is written to a file named config.ini.
Output: Generated config.ini File
[General]
debug = True
log_level = info[Database]
db_name = example_db
db_host = localhost
db_port = 5432
In this example, the configparser module is used to read values from the config.ini file. The retrieved values include boolean, string, and integer data types. These values are stored in a dictionary and printed to the console.
Output:
Debug Mode: True
Log Level: info
Database Name: example_db
Database Host: localhost
Database Port: 5432