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 ? 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
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. :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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",...
|
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...
|
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 &, [ and ] ?
Thanks ...
|
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,...
|
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...
|
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)...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |