How do I use the config parser?

  • Thread starter noagbodjivictor
  • Start date
N

noagbodjivictor

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
 
R

Rob Williscroft

F

Fuzzyman

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
 
L

Larry Bates

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
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top