Updating section in ConfigParser (or an alternative)
Posted
by lyrae
on Stack Overflow
See other posts from Stack Overflow
or by lyrae
Published on 2010-03-23T05:10:23Z
Indexed on
2010/03/23
5:11 UTC
Read the original article
Hit count: 256
python
|configparser
I am making a plugin for another program and so I am trying to make thing as lightweight as possible.
What i need to do is be able to update the name of a section in the ConfigParser's config file.
[project name]
author:john doe
email: [email protected]
year: 2010
I then have text fields where user can edit project's name, author, email and year.
I don't think changing [project name] is possible, so I have thought of two solutions:
1 -Have my config file like this:
[0]
projectname: foobar
author:john doe
email: [email protected]
year: 2010
that way i can change project's name just like another option. But the problem is, i would need the section # to be auto incremented. And to do this i would have to get every section, sort of, and figure out what the next number should be.
The other option would be to delete the entire section and its value, and re-add it with the updated values which would require a little more work as well, such as passing a variable that holds the old section name through functions, etc, but i wouldn't mind if it's faster.
Which of the two is best? or is there another way? I am willing to go with the fastest/lightweight solution possible, doesn't matter if it requires more work or not.
© Stack Overflow or respective owner