ConfigParser : overwrite ?

C

cantabile

Hi, I'm trying and updating an .ini file with ConfigParser but each time
I call 'write', it appends the whole options another time to the file.
For example :
Here's the inital ini file

[section1]
foodir: %(dir)s/whatever
dir: foo

Here's my code :
filename = ...
config = ConfigParser.ConfigParser()
config.read(filename)
config.set('section1', 'dir', 'anotherdir')
f = open(filename, 'r+')
config.write(f)
f.close()

Then I get :

[section1]
foodir: %(dir)s/whatever
dir: anotherdir

[section1]
foodir: %(dir)s/whatever
dir: foo


I tried also with 'w', 'w+', 'a' ...

What's the correct way to avoid this ?
 
R

Robert Kern

cantabile said:
Hi, I'm trying and updating an .ini file with ConfigParser but each time
I call 'write', it appends the whole options another time to the file.
For example :
Here's the inital ini file

[section1]
foodir: %(dir)s/whatever
dir: foo

Here's my code :
filename = ...
config = ConfigParser.ConfigParser()
config.read(filename)
config.set('section1', 'dir', 'anotherdir')
f = open(filename, 'r+')
config.write(f)
f.close()

Then I get :

[section1]
foodir: %(dir)s/whatever
dir: anotherdir

[section1]
foodir: %(dir)s/whatever
dir: foo

I tried also with 'w', 'w+', 'a' ...

Are you sure you tried it with 'w' as the mode?

In [1]: !cat foo.ini
[section1]
foodir: %(dir)s/whatever
dir: foo
In [2]: fn = 'foo.ini'

In [3]: import ConfigParser

In [4]: cfg = ConfigParser.ConfigParser()

In [5]: cfg.read(fn)
Out[5]: ['foo.ini']

In [6]: cfg.set('section1', 'dir', 'anotherdir')

In [7]: f = open(fn, 'w')

In [8]: cfg.write(f)

In [9]: f.close()

In [10]: !cat foo.ini
[section1]
foodir = %(dir)s/whatever
dir = anotherdir

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
C

cantabile

Robert Kern a écrit :
cantabile said:
Hi, I'm trying and updating an .ini file with ConfigParser but each time
I call 'write', it appends the whole options another time to the file.
For example :
Here's the inital ini file

[section1]
foodir: %(dir)s/whatever
dir: foo

Here's my code :
filename = ...
config = ConfigParser.ConfigParser()
config.read(filename)
config.set('section1', 'dir', 'anotherdir')
f = open(filename, 'r+')
config.write(f)
f.close()

Then I get :

[section1]
foodir: %(dir)s/whatever
dir: anotherdir

[section1]
foodir: %(dir)s/whatever
dir: foo

I tried also with 'w', 'w+', 'a' ...


Are you sure you tried it with 'w' as the mode?

In [1]: !cat foo.ini
[section1]
foodir: %(dir)s/whatever
dir: foo
In [2]: fn = 'foo.ini'

In [3]: import ConfigParser

In [4]: cfg = ConfigParser.ConfigParser()

In [5]: cfg.read(fn)
Out[5]: ['foo.ini']

In [6]: cfg.set('section1', 'dir', 'anotherdir')

In [7]: f = open(fn, 'w')

In [8]: cfg.write(f)

In [9]: f.close()

In [10]: !cat foo.ini
[section1]
foodir = %(dir)s/whatever
dir = anotherdir
You are right, it works.
I thought I had tried it ...
Thanks. :)
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top