Search code examples
pythonconfigurationconfigparser

Keep ConfigParser output files sorted


I've noticed with my source control that the content of the output files generated with ConfigParser is never in the same order. Sometimes sections will change place or options inside sections even without any modifications to the values.

Is there a way to keep things sorted in the configuration file so that I don't have to commit trivial changes every time I launch my application?


Solution

  • Looks like this was fixed in Python 3.1 and 2.7 with the introduction of ordered dictionaries:

    The standard library now supports use of ordered dictionaries in several modules. The configparser module uses them by default. This lets configuration files be read, modified, and then written back in their original order.