VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-write-a-configuration-file-in-python/

⇱ How to Write a Configuration File in Python? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Write a Configuration File in Python?

Last Updated : 10 Feb, 2026

A configuration file is a plain text file used to store application settings and parameters in a readable format.

  • Settings are usually written as key–value pairs.
  • The INI (Initialization) format is popular because it is simple and easy to read.
  • Python provides the configparser module to work with INI files.
  • The module allows reading from and writing to configuration files efficiently.

Write a Configuration File in Python

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:

Installing configparser

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.

Creating a Configuration File in Python

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

Reading a Configuration File in Python

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

Comment