Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
INI file reader
# file configfile.py
# create a custom INI file reader and read a file called 'test.ini'
import ConfigParser
class iniReader(ConfigParser.ConfigParser):
def as_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(self._defaults, **d[k])
d[k].pop('__name__', None)
return d
cfg = iniReader()
filename = 'test.ini'
cfg.read(filename)
# options is a dictionary -> access variables like options[section][parameter]
options = cfg.as_dict()
# in test.ini could be something like (remove #s of course)
# ; last modified 1 April 2001 by John Doe
# [owner]
# name=John Doe
# organization=Acme Widgets Inc.
# [database]
# ; use IP address in case network name resolution is not working
# server=192.0.2.62
# port=143
# file="payroll.dat"
; last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Widgets Inc.
[database]
; just another example of a comment
server=192.0.2.62
port=143
file="payroll.dat"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.