Help with ConfigParser

S

Stephen Boulet

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
============

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

Jorge Godoy

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
============

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

Which one? ;-) There are two.

You can list the keys (LHS) and the values (RHS) or both.

import ConfigParser
configFile = open('/etc/g2ctech/cvs.cf')
config = ConfigParser.ConfigParser()
config.readfp(configFile)
config.items('default') [('forbidden_exts', "['.doc', '.xls', '.ppt']"), ('cvsroot', '/home/cvs')]
config.items('get_data_from_mail')
[('maildir',
'/home/godoy/empresa/clientes/dataprev/cvs-import/tests/sample')]


Default is a section as well as get_data_from_mail.


Be seeing you,
 
D

David Goodger

Stephen said:
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
============

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

What are you looking up, and what's the invariant (what's the lookup
key)? I'd write the config file like this:

[Backups]
photos = /home/stephen/photos/digital camera
taxes = /home/stephen/documents/tax documents

Lookup keys are on the left, and configuration data is on the right.

From your example though, it looks like you don't have specific
lookup keys. You're just storing an arbitrary set of paired data.
You could use an "items('Backups')" method call to get a list of
(key, value) pairs which you could iterate over.

-- David Goodger
 
P

Peter Otten

Stephen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top