Seven Ways to Read and Write Configuration Files in Python

Mastering Python can change your life; using Python effectively can greatly enhance your efficiency!—— Follow me to unlock a world of efficiency with Python.AI has now become an indispensable part of everyone’s life, as essential as water, electricity, and gas. Developing various application tools using large models and sharing them with others is a common practice. However, the API keys for these large models need to be configured, models need to be selected, and the loading paths and parameters for self-trained models also require configuration. These configuration files can be in config.ini format, config.json, config.yaml, as well as xml, toml, py, and .env formats. Today, we will discuss the main methods for reading and writing configurations.

Comprehensive Comparison of Various Configuration Methods

Format Readability Complexity Security Type Support Applicable Scenarios
INI High Low High Basic Types Simple configuration, grouped configuration
JSON Medium Medium High Rich Cross-language configuration, data exchange
YAML High High Medium Rich Complex configurations, scenarios requiring comments
XML Low High High Rich Enterprise-level standardized configurations
.env High Low High Strings Sensitive information, environment variables
TOML High Medium High Rich Python projects, balancing simplicity and structure
Python File Medium High Low Fully supported Framework configuration, dynamic calculation of configurations

Method 1: INI File

Seven Ways to Read and Write Configuration Files in Python

Advantages: Simple structure, easy to read and edit; natively supports the concept of sections, suitable for grouped configurations; Python’s standard library includes configparser, no additional installation required.

Disadvantages: Does not support nested structures, making complex configuration organization difficult; limited data types; lacks native support for complex data structures like lists; not suitable for storing large or complex configuration data.

import configparser
def handle_ini_file():    # Write INI file    config = configparser.ConfigParser()    # Add sections and key-value pairs    config['DEFAULT'] = {        'ServerAliveInterval': '45',        'Compression': 'yes',        'CompressionLevel': '9'    }    config['bitbucket.org'] = {}    config['bitbucket.org']['User'] = 'hg'    config['topsecret.server.com'] = {}    topsecret = config['topsecret.server.com']    topsecret['Port'] = '50022'     # Automatically converted to string    topsecret['ForwardX11'] = 'no'  # Automatically converted to string    # Write to file    with open('myconfig.ini', 'w') as f:        config.write(f)    # Read INI file    config_read = configparser.ConfigParser()    config_read.read('myconfig.ini')    # Various ways to get values    print("INI file content:")    print(f"ServerAliveInterval: {config_read['DEFAULT']['ServerAliveInterval']}")    print(f"bitbucket User: {config_read.get('bitbucket.org', 'User')}")    print(f"Port: {config_read.getint('topsecret.server.com', 'Port')}")  # Convert to integer    print(f"ForwardX11: {config_read.getboolean('topsecret.server.com', 'ForwardX11')}")  # Convert to boolean

Method 2: JSON File

Advantages: Clear structure, supports nested and complex data structures; natively supports common Python data types like lists and dictionaries; excellent cross-language support, almost all programming languages have JSON parsing libraries; Python’s standard library includes the json module, no additional installation required; suitable for data exchange and storage.

Applicable Scenarios: Data exchange between programs; configurations that need to be used across languages; relatively complex structures that do not require comments.

import json
def handle_json_file():    # Prepare data    data = {        "name": "John",        "age": 30,        "city": "New York",        "is_student": False,        "hobbies": ["reading", "gaming", "hiking"],        "address": {            "street": "123 Main St",            "zipcode": "10001"        }    }    # Write to JSON file    with open('myconfig.json', 'w') as f:        json.dump(data, f, indent=4)  # indent parameter for formatted output    # Read JSON file    with open('myconfig.json', 'r') as f:        data_read = json.load(f)    print("JSON file content:")    print(f"Name: {data_read['name']}")    print(f"Age: {data_read['age']}")    print(f"Hobbies: {', '.join(data_read['hobbies'])}")    print(f"Zipcode: {data_read['address']['zipcode']}")    print()

Method 3: YAML File

Advantages: Concise syntax, excellent readability; supports comments for configuration explanations; supports complex nested structures and various data types; automatically retains data type information; more flexible and concise than JSON.

Disadvantages: Not part of Python’s standard library, requires additional installation of pyyaml; sensitive to indentation, which can lead to parsing errors due to formatting issues; compatibility issues may exist between different parsers; security needs to be considered.

import yaml
def handle_yaml_file():    # Configuration data    data = {        "name": "Alice",        "age": 25,        "city": "London",        "is_student": True,        "hobbies": ["painting", "dancing"],        "address": {            "street": "456 Oak Ave",            "zipcode": "SW1A 1AA"        }    }    # Write to YAML file    with open('myconfig.yaml', 'w') as f:        yaml.dump(data, f, sort_keys=False)  # sort_keys=False keeps the order of keys    # Read YAML file    with open('myconfig.yaml', 'r') as f:        data_read = yaml.safe_load(f)  # It is recommended to use safe_load instead of load for safety    print("YAML file content:")    print(f"Name: {data_read['name']}")    print(f"Age: {data_read['age']}")    print(f"Hobbies: {', '.join(data_read['hobbies'])}")    print(f"Zipcode: {data_read['address']['zipcode']}")

Applicable Scenarios:

Complex application configuration files

Configurations requiring explanatory comments

Configuration files manually edited by developers

Seven Ways to Read and Write Configuration Files in Python

Method 4: TOML File

TOML is a format designed for configuration files, combining the simplicity of INI with the structural advantages of JSON, and is one of the recommended configuration formats by Python.

import toml
# Write to TOML
data = {    "title": "TOML config",    "owner": {        "name": "Tom Preston-Werner",        "dob": "1979-05-27T07:32:00-08:00"    },    "database": {        "server": "192.168.1.1",        "ports": [8001, 8001, 8002]    }}
with open("config.toml", "w") as f:    toml.dump(data, f)
# Read TOML
with open("config.toml", "r") as f:    config = toml.load(f)
print('TOML configuration information:', config["database"]["server"])

Method 5: XML File

XML is a markup language with a strict structure, suitable for complex configurations, but its syntax is relatively cumbersome.

Advantages: Strict structure, supports complex nesting and namespaces, suitable for enterprise-level applications.

Disadvantages: Redundant syntax, poorer readability, and relatively complex parsing.

Applicable Scenarios: Configurations requiring strict specifications, such as Java ecosystem migration projects.

import xml.etree.ElementTree as ET
# Write to XML
root = ET.Element("config")
server = ET.SubElement(root, "server")
ET.SubElement(server, "host").text = "localhost"
ET.SubElement(server, "port").text = "8080"
tree = ET.ElementTree(root)
tree.write("config.xml")
# Read XML
tree = ET.parse("config.xml")
root = tree.getroot()
host = root.find("server/host").text
port = root.find("server/port").text
print(host)
print('Port', port)

Method 6: Environment Variables (.env File)

Manage environment variables using the python-dotenv library, suitable for storing sensitive configurations (e.g., API keys).

Installation method

pip install python-dotenv

from dotenv import load_dotenv
import os
# Load .env file
load_dotenv()  # Loads the .env file from the current directory by default
# Read environment variables
api_key = os.getenv("API_KEY")
database_url = os.getenv("DATABASE_URL")
print('API Key:', api_key)
print('URL:', database_url)

Method 7: Python File

Direct configuration

Brief

Seven Ways to Read and Write Configuration Files in Python

For most modern Python projects, YAML and TOML are excellent choices that balance readability and functionality, while .env is the best practice for managing sensitive information.

Historical Python Articles:

Basic articles for learning Python

Fundamentals (suitable for beginners)

Essential Python OS Standard Library

Overview of Python Built-in Functions, Simple and Intuitive

Applications of Regular Expressions in Office

Essential Python sys Standard Library

Python Knowledge Handy Reference

Using Domestic Sources for pip to Speed Up Python Library Installation

Usage of Python Lambda

Essential Python Socket

Four Ways to Package Python Projects into EXE

Common Programming Reference Atlas for Python

Common Programming Knowledge Reference Atlas for Python II

Four Progress Bars in Python

Timestamp Calculation in Python

Seven Ways to Read and Write Configuration Files in Python

Mathematics and Computation

Topic: The Three Musketeers of Python Mathematical Operations – Pandas

Topic: The Three Musketeers of Python Mathematical Operations – Numpy

Topic: The Three Musketeers of Python Mathematical Operations – A Brief Discussion on Scipy

The Wonderful Collision of Python and Mathematics, Making Computation Easy

Data Analysis and Visualization

Python Visualization with Bokeh Library

Python Visualization with Pyecharts

Matplotlib Turns Mathematics into Visualization

Python Visualization Charts with Seaborn Library

The Adorable Python Hand-drawn Chart Library – CuteCharts

Web Scraping

Notes on the New Version of Selenium 4.0

Essential Python BeautifulSoup Library

Practical: Easily Implement HTTP Requests with Requests Library

Scrapy to be Supplemented

Urllib to be Supplemented

Playwright Library – Let Python Help You with Browser Tasks

Image Processing

Essential Python OpenCV Library

Python Image Processing Library – PillowImageio to be SupplementedNatural Language Processing

Python Chinese Word Segmentation with Jieba Library

Python Natural Language Processing with spaCy Library

NLTK to be Introduced

Office Applications

Python Task Scheduling Tool Schedule, Let It Help You Work on Time

Automate Word Document Processing with Python-docx

Five Ways to Write Data into CSV Files with Python

Several Ways to Read and Write Files in Python

Getting Started with Python Documentation Auto-generation using Sphinx Library

Python’s Swiss Army Knife for PDF Processing – PyMuPDF Library

PyPDF2 to be Supplemented

Python Automation Library PyAutoGUI

Industrial ApplicationsPython’s Efficiency Application in Mechanical Drawing with CadQuery LibrarySoftware Interfaces and GamesPyQt Development Library to be IntroducedTkinter Interface Library to be IntroducedUsing Pygame to Teach You How to Create a Plane Battle GameProject Engineering Related

Getting Started with Python’s Automated Testing Library Pytest

Python Project Logging with Loguru Library

Essential Python Threading Library

Using Lightweight Database SQLite in Python Projects

Essential Python Cryptography Library

Exploring the Faker Library, a Powerful Tool for Generating Various Test Data

Thread Pool RelatedConcurrent.futures to be Introduced

Fun Projects

Python Takes You to Explore the Stars and the Sea with Skyfield Library

Python’s Super Fun Library – Turtle Graphics

Deep Use of Python in Music Creation [For Audio Processing Reference]

System Hardware, etc.

Python’s System Monitoring and Process Management Tool – Psutil Library

Platformdirs to be Supplemented

Pypiwin32to be Supplemented

PySerialto be Supplemented

PyWiFito be Supplemented

Python Web Development Related

Flask to be IntroducedDjango to be IntroducedVideo ProcessingFFmpeg-python to be UpdatedUsing Python to Accurately Dub Videos in BulkPython Development, a Tool for Translating Chinese Videos into 30+ Languages

Others

Python Application Universe, All Uses Explained

Organizing Personal Library Files Installed in pip list

Popular Articles

Python Video Processing Tools, Assisting Learning and Recreation

Comprehensive Overview of Domestic GPU Rental Platforms (The Most Comprehensive on the Internet, No Exceptions)

Notes on the New Version of Selenium 4.0

Four Progress Bars in Python

Python’s Super Fun Library – Turtle Graphics

Essential Python BeautifulSoup Library

Essential Python OpenCV Library

Essential Python Threading Library

Essential Python OS Standard Library

Using Python to Read Large CSV Files and Search Content

Python Task Scheduling Tool Schedule, Let It Help You Work on Time

Creating a Tomato Timer EXE with Python to Improve Work Efficiency and Easily Manage Time

Using Domestic Sources for pip to Speed Up Python Library Installation

Five Ways to Write Data into CSV Files with Python

Several Ways to Read and Write Files in Python

Four Ways to Package Python Projects into EXE

Python Automation Library PyAutoGUI

Keyboard and Mouse Automation with Pynput Library

Agent Historical Articles

Detailed Analysis and Operation Modification of Agent OWL Architecture

Detailed Breakdown of OpenManus Agent

AI Historical ArticlesRecord of Deploying Speech-to-Text Deep Learning Tools on Local CPU

Guide to Using Python to Call DeepSeek API for Multi-turn Conversations

Alternative Solutions for Busy DeepSeek Servers (Ongoing Follow-up)

The Most Comprehensive Alternative Solutions and Evaluations for DeepSeek on the Internet, No Exceptions

Building Your Own DeepSeek R1 and Knowledge Base – A Step-by-Step Tutorial

Instructions for Using Python to Call Suno API

Comprehensive Overview of Domestic GPU Rental Platforms (The Most Comprehensive on the Internet, No Exceptions)

Recreating Your Digital Avatar – Voice Avatar

Case Applications

A Tool for Precisely Inserting Video Subtitles Created with Python

Python Video Processing Tools, Assisting Learning and Recreation

Implementing 10 Average Algorithms with Python

Creating Your Own Piano Keyboard with Pygame (Part 1)

Implementation of the Knapsack Algorithm in Python

Practical: Implementing a Device QR Code Generator with Python

Practical: I Used MoviePy to Create a Simplified Version of JianYing

Using Python to Read Large CSV Files and Search Content

Using Python to Automatically Send Emails for Remote Device Maintenance

Python Task Scheduling Tool Schedule, Let It Help You Work on Time

Creating a Tomato Timer EXE with Python to Improve Work Efficiency and Easily Manage Time

An Unconventional Python Application Practical Case

Using Python to Help You Research and Make Money

Deep Use of Python in Music Creation

Efficiently Using Python to Achieve Your Labeling Freedom

Implementing 10 Average Algorithms with Python

Leave a Comment