configparser:
configparser
suggest changeThis module provides the ConfigParser class which implements a basic configuration language in INI files. You can use this to write Python programs which can be customized by end users easily.
Syntax
- Each new line contains a new key value pair separated by the = sign
- Keys can be separated in sections
- In the INI file, each section title is written between brackets: []
Remarks
All return values from ConfigParser.ConfigParser().get are strings. It can be converted to more common types thanks to eval
Creating configuration file programmatically
Configuration file contains sections, each section contains keys and values. configparser module can be used to read and write config files. Creating the configuration file:-
import configparser
config = configparser.ConfigParser()
config['settings']={'resolution':'320x240',
'color':'blue'}
with open('example.ini', 'w') as configfile:
config.write(configfile)
The output file contains below structure
[settings]
resolution = 320x240
color = blue
If you want to change particular field ,get the field and assign the value
settings=config['settings']
settings['color']='red'
Basic usage
In config.ini:
[DEFAULT]
debug = True
name = Test
password = password
[FILES]
path = /path/to/file
In Python:
from ConfigParser import ConfigParser
config = ConfigParser()
#Load configuration file
config.read("config.ini")
# Access the key "debug" in "DEFAULT" section
config.get("DEFAULT", "debug")
# Return 'True'
# Access the key "path" in "FILES" destion
config.get("FILES", "path")
# Return '/path/to/file'
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
3List
22Reduce
27Classes
28Counting
31Set
42Tuple
45Enum
60setup.py
62Sockets
89urllib
92Idioms
104Stack
105Profiling
109Logging
111os module
118Mixins
120ArcPy
123Websockets
126Arrays
128Polymorphism
1322to3 tool
135Unicode
138Neo4j
140Curses
141Templates
145heapq
146tkinter
154Audio
155pyglet
156queue module
157ijson
160Flask
161Groupby
163pygame
165hashlib
166Gzip
167ctypes
170configparser
179sys module
185pyaudio
186shelve
191Contributors