ConfigObj

5.0.9 · active · verified Thu Apr 09

ConfigObj is a Python library for reading, writing, and validating configuration files. It supports a syntax similar to .ini files but adds features like nested sections, list handling, and comprehensive validation. It is currently at version 5.0.9 and is actively maintained with infrequent but important updates, often addressing security concerns or Python version compatibility.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a ConfigObj instance, populate it with data (including nested sections and lists), write it to a file, and then read it back. It concludes with cleanup of the created file.

from configobj import ConfigObj
import os

# 1. Create and write a config file
config = ConfigObj()
config.filename = "example.ini"

config['Section One'] = {
    'key1': 'value1',
    'key2': ['itemA', 'itemB', 'itemC']
}

config['Section Two'] = {}
config['Section Two']['nested_key'] = 'nested value'

config.write()

print(f"Config file 'example.ini' created.\n")

# 2. Read the config file
read_config = ConfigObj('example.ini')

print(f"Read config content:\n{read_config.dict()}\n")
print(f"Value from Section One, Key1: {read_config['Section One']['key1']}")
print(f"List from Section One, Key2: {read_config['Section One']['key2']}")

# 3. Clean up the created file
os.remove("example.ini")
print("example.ini removed.")

view raw JSON →