ConfigParser : overwrite ?

Discussion in 'Python' started by cantabile, Jul 17, 2005.

  1. cantabile

    cantabile Guest

    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 ?
    cantabile, Jul 17, 2005
    #1
    1. Advertising

  2. cantabile

    Robert Kern Guest

    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


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Jul 17, 2005
    #2
    1. Advertising

  3. cantabile

    cantabile Guest

    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. :)
    cantabile, Jul 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Um9iZXJ0?=

    Deployment in 2.0 - How To Not Overwrite web.config

    =?Utf-8?B?Um9iZXJ0?=, Nov 2, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    4,429
    Juan T. Llibre
    Nov 3, 2005
  2. VisionSet
    Replies:
    1
    Views:
    4,160
    Adam Lipscombe
    Sep 2, 2003
  3. zalla rouge
    Replies:
    0
    Views:
    460
    zalla rouge
    Sep 2, 2003
  4. shruti tiwari via JavaKB.com

    overwrite existing value of a textbox with null

    shruti tiwari via JavaKB.com, Apr 2, 2005, in forum: Java
    Replies:
    1
    Views:
    656
    David.Federman
    Apr 3, 2005
  5. Replies:
    3
    Views:
    410
Loading...

Share This Page