468,288 Members | 1,989 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,288 developers. It's quick & easy.

How do I use the config parser?

Hi,
I need a specific example. I have seen the docs, but I don't all the
stuffs there.

So basically, I need my config file to be created and read by my
script.

Here is a snippet

# read old actions
from ConfigParser import ConfigParser

fp = open(makepath('App\qt_actions.conf'))
configdict = ConfigParser()
configdict.readfp(fp)
Now I want to know how to read a section, a section attribute's value,
and to write thoses back after reading.

Thanks

May 5 '07 #1
3 9219
wrote in news:11**********************@e65g2000hsc.googlegr oups.com in
comp.lang.python:
Hi,
I need a specific example. I have seen the docs, but I don't all the
stuffs there.
from ConfigParser import ConfigParser
Now I want to know how to read a section, a section attribute's value,
and to write thoses back after reading.
ConfigParser is derived from RawConfigParser, so you need
to look at RawConfigParser's docs here:

http://docs.python.org/lib/RawConfigParser-objects.html
Rob.
--
http://www.victim-prime.dsl.pipex.com/
May 5 '07 #2
On May 5, 8:13 pm, noagbodjivic...@gmail.com wrote:
Hi,
I need a specific example. I have seen the docs, but I don't all the
stuffs there.

So basically, I need my config file to be created and read by my
script.

Here is a snippet

# read old actions
from ConfigParser import ConfigParser

fp = open(makepath('App\qt_actions.conf'))
configdict = ConfigParser()
configdict.readfp(fp)

Now I want to know how to read a section, a section attribute's value,
and to write thoses back after reading.
You could do it simply with ConfigObj ( http://www.voidspace.org.uk/python/configobj.html
):

from configobj import ConfigObj

config = ConfigObj(filename)
section = config['Section Name']
value = section['value']

.....

section['value'] = newValue
config.write()

All the best,
Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml

Thanks

May 5 '07 #3
no*************@gmail.com wrote:
Hi,
I need a specific example. I have seen the docs, but I don't all the
stuffs there.

So basically, I need my config file to be created and read by my
script.

Here is a snippet

# read old actions
from ConfigParser import ConfigParser

fp = open(makepath('App\qt_actions.conf'))
configdict = ConfigParser()
configdict.readfp(fp)
Now I want to know how to read a section, a section attribute's value,
and to write thoses back after reading.

Thanks
The best place to start is always:

import ConfigParser
help(ConfigParser)
Example:

section='INIT'
option='logfile'

logfile=configdict.get(section, option)

most of the methods are self explanitory.

-Larry
May 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.