faqts : Computers : Programming : Languages : Python : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

42 of 46 people (91%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Searching an example on how to use configparser in python?

Jul 17th, 2000 14:26
unknown unknown, Olmy


assuming you have a config file called test.conf and it contains the 
following:
[DEFAULT]
name = pythontest
path = /tmp/test
then you should be able to use the following quickie test script:
from ConfigParser import *
configdict = ConfigParser()
configdict.read('test.conf')
print configdict.defaults().keys()
print configdict.defaults()['path']
path = configdict.defaults()['path']
print path
that should give you a quick idea of how to load your config file 
in ConfigParser's dictionary format.
See the following URL for the reference documentation on what else 
you can do with ConfigParser:
http://www.python.org/doc/current/lib/module-ConfigParser.html
Pay special attention on how to handle RFC822 sections and how to handle 
the "%" expansions.