Skip to content

Instantly share code, notes, and snippets.

@j-faria
Created January 9, 2014 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-faria/8341187 to your computer and use it in GitHub Desktop.
Save j-faria/8341187 to your computer and use it in GitHub Desktop.
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