By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

How do I use the config parser?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.