Help with ConfigParser

Peter Otten __peter__ at web.de
Sun May 2 08:22:49 EDT 2004


Stephen Boulet wrote:

> I'm having a bit of trouble getting my head around the ConfigParser
> module. I have a very simple configuration file; maybe the easiest thing
> to do would be to show that:
> 
> ============
> # Add a local directory to be backed up followed
> # by the directory name on the FTP server.
> #
> # Example: /home/joe/digital photos = photos
> 
> [Backups]
> /home/stephen/photos/digital camera = photos
> /home/stephen/documents/tax documents = taxes

Just the other way round:

photos=/home/...

> ============
> 
> I just want to retrieve the information in the backups section. Thanks.
> 

A minimal example:

from ConfigParser import ConfigParser

CONFIG_FILE = "boulet.ini"

# generate sample data
f = file(CONFIG_FILE, "w")
f.write("""
[Backups]
photos=/home/stephen/photos/digital camera
taxes=/home/stephen/documents/tax documents
""")
f.close()

# retrieve configuration
p = ConfigParser()
p.read(CONFIG_FILE)

# a single value
print "Photos:", p.get("Backups", "photos")

# a complete section
print "Backups:"
for key, value in p.items("Backups"):
    print "\t%r --> %r" % (key, value)

Peter




More information about the Python-list mailing list