How can I read, write, and create an INI file with Python3?
I have an INI file, FILE.INI, with the following content:
default_path = "/path/name/"
default_file = "file.txt"
In Python, I want to:
- Read the file and create it if it doesn’t exist.
- Retrieve the value of
default_path
.
- Update or append new values to the INI file.
Here’s what I expect to do in the Python file:
# Read file and create if it doesn't exist
config = iniFile('FILE.INI')
# Get "default_path"
print(config.default_path) # Should print "/path/name/"
# Create or Update
config.append('default_path', 'var/shared/')
config.append('default_message', 'Hey! help me!!')
The updated FILE.INI should look like:
default_path = "var/shared/"
default_file = "file.txt"
default_message = "Hey! help me!!"
How can I achieve this in Python, specifically for handling Python ini file operations?
If you’re new to handling INI files in Python, the built-in configparser
module is a great place to start. It’s simple, efficient, and requires no additional installations. Here’s an example:
import configparser
# Initialize the config parser
config = configparser.ConfigParser()
# Read the INI file (or create it if it doesn’t exist)
config.read('FILE.INI')
# Access the 'default_path' value
print(config['DEFAULT']['default_path']) # Output: "/path/name/"
# Update the 'default_path' value
config['DEFAULT']['default_path'] = '/var/shared/'
# Add a new entry for 'default_message'
config['DEFAULT']['default_message'] = 'Hey! help me!!'
# Save the changes back to the INI file
with open('FILE.INI', 'w') as configfile:
config.write(configfile)
This should update your FILE.INI file as expected. Want more advanced handling? Let’s dive into alternative libraries next.
If you’re comfortable using a third-party library, ConfigObj offers a more intuitive and user-friendly interface for managing INI files. Plus, it comes with features like automatic type conversion! Here’s how to use it:
from configobj import ConfigObj
# Load the INI file (or create it if it doesn’t exist)
config = ConfigObj('FILE.INI')
# Retrieve the value of 'default_path'
print(config['default_path']) # Output: "/path/name/"
# Update 'default_path' and add a new key-value pair
config['default_path'] = '/var/shared/'
config['default_message'] = 'Hey! help me!!'
# Save changes back to the INI file
config.write()
To install ConfigObj, simply run:
pip install configobj
This library is particularly handy if you want less verbosity compared to configparser
. Need to work with sections in your INI file? The next example has you covered.
When your INI file has multiple sections, you can manage them easily using configparser
. This is especially useful for larger or more organized configurations. Let me show you:
import configparser
# Initialize the ConfigParser
config = configparser.ConfigParser()
# Read the INI file
config.read('FILE.INI')
# Access and print the value of 'default_path' in the 'Settings' section
print(config['Settings']['default_path']) # Output: "/path/name/"
# Update the 'default_path' and add a new key 'default_message' in 'Settings'
config['Settings']['default_path'] = '/var/shared/'
config['Settings']['default_message'] = 'Hey! help me!!'
# Save the updated file
with open('FILE.INI', 'w') as configfile:
config.write(configfile)
This approach makes it easy to organize your INI files with sections like [Settings]
, [User Preferences]
, etc. Just ensure you structure your INI file properly to reflect these sections.