[Tutor] Config files & getopt()

Karthik Gurumurthy karthikg@aztec.soft.net
Fri, 28 Dec 2001 10:12:35 +0530

see of this is useful...
it creates a config file and then reads from it.

>>> from ConfigParser import *
>>> fp = open("test.conf","w+")
>>> c = ConfigParser()
>>> c.add_section("server")
>>> c.set("server","dsn","octago")
>>> c.set("server","password","octago")
>>> c.set("server","userid","octago")
>>> c.add_section("properties")
>>> c.set("properties","log","d:\\temp\\logs")
>>> c.set("properties","templates","d:\\temp\\templates")
>>> c.write(fp)
>>> fp.close()
........Now read from the same.....

>>> f = open("test.conf","r")
>>> c.readfp(f)
>>> c.sections()
['server', 'properties']
>>> c.get('server','dsn')
>>> c.get('server','password')
>>> c.get('server','userid')
>>> c.get('properties','log')
>>> c.get('properties','templates')

userid = octago
password = octago
dsn = octago

log = d:\temp\logs
templates = d:\temp\templates

never used getopt though

-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
Sent: Friday, December 28, 2001 5:05 AM
To: tutor@python.org
Subject: [Tutor] Config files & getopt()

First, thanks for the help on IP sorting. I added it to the program and it
runs great.

If it gets to the point I like it, I may post it here as an example of how
-NOT- to do data reduction and generate statistical data on a million+ line
file :)


I am still tinkering with other stuff too...

As a background, I have -0- programming experience and am learning both
Python and programming from scratch. So far so good, however I am having
problems understanding two things I need for something I am writing.

The first is getopt(). I just don't get it. Can someone give a better
description of it, other than is presented in the module, (with examples
too! :) )? I have tried that one, the O'Reily book and searching
online..and I am still kinda lost...although I do know
getopt(arg,option(,long options)) :)

The second, is I would like to add a config file to  program..something
along the lines of an apache httpd.conf file, to specify directories and
the like. I checked out ConfigParser...and I just dont get it..

Ok..so I am feeling moronic today :)

Any kind soul wanna hand hold a true newbie?

Thanks in advance..


Tutor maillist  -  Tutor@python.org