how to write special value to a config file with ConfigParser

L

Lialie

Hello,all
I found it easy to read configures from a config file.
But how can I set a special value to an item or write it into the
original file?
I have tried this:

import ConfigParser
config = ConfigParser.ConfigParser()
config.read('a.conf')
config.get('Main', 'Something') # That is OK.
config.set('Main', 'Something', '2') # without any fault
#fp = open('a.conf') # can not be done, raise Error
#fp = open('a.conf', 'a') # Something set to 2, but it copies the all items
#config.write(fp)


# How can I do it?
 
P

pavel.starek

Hi,

I have tried this:

<pre>
import ConfigParser
fp = file("test.conf","w+")
cp = ConfigParser.ConfigParser()
cp.add_section("Section1")
cp.set("Section1","Value1",12345)
cp.write(fp)
</pre>

and works fine for me.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top