Help with ConfigParser

Discussion in 'Python' started by Stephen Boulet, May 2, 2004.

  1. 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.

    --
    Stephen
    From here to there
    and there to here,
    funny things are everywhere. -- Dr Seuss
     
    Stephen Boulet, May 2, 2004
    #1
    1. Advertising

  2. Stephen Boulet

    Jorge Godoy Guest

    On Dom 02 Mai 2004 08:49, 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
    > ============
    >
    > 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')]
    >>> config.get('default', 'cvsroot')

    '/home/cvs'
    >>>



    Default is a section as well as get_data_from_mail.


    Be seeing you,
    --
    Godoy. <>
     
    Jorge Godoy, May 2, 2004
    #2
    1. Advertising

  3. 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
    > ============
    >
    > 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
     
    David Goodger, May 2, 2004
    #3
  4. Stephen Boulet

    Peter Otten Guest

    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
     
    Peter Otten, May 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Gass
    Replies:
    0
    Views:
    470
    Dan Gass
    Sep 13, 2004
  2. mwt
    Replies:
    5
    Views:
    523
    Kent Johnson
    Mar 15, 2006
  3. Alexandre CONRAD
    Replies:
    1
    Views:
    298
    Fuzzyman
    Apr 6, 2006
  4. Help with ConfigParser

    , Oct 2, 2006, in forum: Python
    Replies:
    3
    Views:
    347
    Enrico
    Oct 2, 2006
  5. Replies:
    3
    Views:
    447
Loading...

Share This Page