473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ConfigParser : overwrite ?

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 ?
Jul 21 '05 #1
2 1612
cantabile wrote:
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
rk***@ucsd.edu

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

Jul 21 '05 #2
Robert Kern a écrit :
cantabile wrote:
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. :)
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Greg Krohn | last post by:
I'm trying to subclass ConfigParser so I can use a custom __read method (the custom format doesn't conform to RFC 822) when needed. Needless to say, it's not working as expected. In the...
1
by: Martin Maney | last post by:
Summary: with batteries like these, get a longer extension cord? <transcript from interactive session> Python 2.2.1 (#1, Sep 7 2002, 14:34:30) on linux2 Type "help", "copyright",...
2
by: Roy H. Berger | last post by:
If I want to subclass ConfigParser and changed the optionxform method to not return things in lower case wouldn't I just need the following code in my subclasss module? from ConfigParser import...
11
by: Manlio Perillo | last post by:
Regards. Since sections in CongiParser files are delimited by , why there is not an escape (and unescape) function for escaping &, characters to &amp;, &lbrack; and &rbrack; ? Thanks ...
2
by: rzed | last post by:
I am working with PythonCard in one of my apps. For its purposes, it uses an .ini file that is passed to ConfigParser. For my app, I also need configuration information, but for various reasons,...
10
by: Terry Carroll | last post by:
It looks like ConfigParser will accept a list to be writing to the *.ini file; but when reading it back in, it treats it as a string. Example: ############################### import...
1
by: pipehappy | last post by:
Hello everyone: I came across the module ConfigParser and can use it correctly. import ConfigParser fp = open('test.cfg','w+') config = ConfigParser.ConfigParser() config.readfp(fp)...
4
by: Danil Dotsenko | last post by:
Wrote a little "user-friedly" wrapper for ConfigParser for a KDE's SuperKaramba widget. (http://www.kde-look.org/content/show.php?content=32185) I was using 2.4.x python docs as reference and...
4
by: Phoe6 | last post by:
Hi, I have a configfile, in fact, I am providing a configfile in the format: Name: Foo Author: Bar Testcases: tct123
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.