how to write special value to a config file with ConfigParser

Discussion in 'Python' started by Lialie, Apr 23, 2006.

  1. Lialie

    Lialie Guest

    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?
    Lialie, Apr 23, 2006
    #1
    1. Advertising

  2. Lialie

    Guest

    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.
    , Apr 23, 2006
    #2
    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. kai
    Replies:
    1
    Views:
    272
    Larry Bates
    May 5, 2006
  2. CSharpner
    Replies:
    0
    Views:
    996
    CSharpner
    Apr 9, 2007
  3. Replies:
    3
    Views:
    411
  4. Chuck

    Config & ConfigParser

    Chuck, Mar 5, 2013, in forum: Python
    Replies:
    11
    Views:
    217
    Neil Cerutti
    Mar 7, 2013
  5. Tim Chase
    Replies:
    0
    Views:
    85
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page