Is there a ConfigParser which keeps comments

Cameron Simpson cs at
Thu Mar 15 22:42:24 CET 2012

On 14Mar2012 13:13, Tim Chase <python.list at> wrote:
| On 03/14/12 12:06, Terry Reedy wrote:
| > On 3/14/2012 6:07 AM, Gelonida N wrote:
| >> Now I'm looking for a library, which behaves like config parser, but
| >> with one minor difference.
| >>
| >> The write() mehtod should keep existing comments.
| >
| > Assuming that you have not overlooked anything, I would just subclass
| > ConfigParser with an altered write method.
| It would require a lot more than that.  It would entail changing 
| the reading as well so that it preserved the comments as well as 
| the order of sections & keys, and a way of storing those 
| associated comments in sequence.  I looked into it a fair while 
| back and it was a LOT more work than I cared to do for minimal 
| gain.  I wimped out and just documented it with "If you use the 
| ability to (re)write a configuration file, it will not keep any 
| comments or ordering from any original sources."

A low cost approach might be to patch the file instead of transcribing
the in-memory state. Not the same semantics, but it would not be too
hard to add a patch_config(filename, section, setting, value) that read
the old file and wrote a new one with an adjusted section, ignoring the
in-memory state (indeed, on that basis the siganture isn't a method but
a standalone function).

The logic is easy enough that I even wrote a shell script in 2004 to do
essentially this:

One could imagine an efficient python implementation and a ConfigParser
subclass that patched the file if a setting got changed, or had a .patch
method to apply particular setting changes as desired.

Cameron Simpson <cs at> DoD#743

From sci.physics:
tron at
  The only problem is, how do you send a message from Earth to Mars
  instantly?  Does anyone have any ideas about where we can start?
John Baez <baez at
  Just use a coordinate system in which the point at which the message is
  received has the same t coordinate as the point at which the message was sent.

More information about the Python-list mailing list