43

How can one write comments to a given file within sections?

If I have:

import ConfigParser
with open('./config.ini', 'w') as f:
    conf = ConfigParser.ConfigParser()
    conf.set('DEFAULT', 'test', 1)
    conf.write(f)

I will get the file:

[DEFAULT]
test = 1

But how can I get a file with comments inside [DEFAULT] section, like:

[DEFAULT]
; test comment
test = 1

I know I can write codes to files by doing:

import ConfigParser
with open('./config.ini', 'w') as f:
    conf = ConfigParser.ConfigParser()
    conf.set('DEFAULT', 'test', 1)
    conf.write(f)
    f.write('; test comment') # but this gets printed after the section key-value pairs

Is this a possibility with ConfigParser? And I don't want to try another module because I need to keep my program as "stock" as possible.

tshepang
  • 12,111
  • 21
  • 91
  • 136
razvanc
  • 930
  • 2
  • 11
  • 18
  • After considering ConfigParser for writing configuration files, I decided to write my file using old standard file interface `f = open('test.ini', 'w'); f.write('blabla')` because the ConfigParser module doesn't even write in a predifined order (because it's using dictionaries, even though one of the examples states that the writing is made in some sort of order: [python docs](http://docs.python.org/library/configparser.html#examples)) – razvanc Jul 08 '11 at 10:16
  • If you are still around I'll suggest you write a short answer regarding this and mark it as accepted. I read the proposed answer even after this comment and came to the same conclusion... but took me a while and I have even voted up your selected solution... – estani Dec 07 '12 at 09:59

5 Answers5

36

You can use the allow_no_value option if you have Version >= 2.7

This snippet:

import ConfigParser

config = ConfigParser.ConfigParser(allow_no_value=True)
config.add_section('default_settings')
config.set('default_settings', '; comment here')
config.set('default_settings', 'test', 1)
with open('config.ini', 'w') as fp:
    config.write(fp)


config = ConfigParser.ConfigParser(allow_no_value=True)
config.read('config.ini')
print config.items('default_settings')

will create an ini file like this:

[default_settings]
; comment here
test = 1
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • As a side note, this works as long as you're not trying to write comments in the 'DEFAULT' section - there's a check in the write function to avoid writing " = None", but only when not writing out the 'DEFAULT' section. – Jordan Evens Jan 15 '15 at 13:31
  • Good point. Fortunately, in Python 3.6, the `configparser` module does not have this strange behavior. Thus, `config = configparser.ConfigParser({"; comment": None}, allow_no_value=True)` inserts a comment in the default section, without any trailing `= None`. In order to guarantee a correct ordering of the output, a `collections.OrderedDict` is required (instead of a `dict` as in my example). – Eric O. Lebigot Jan 25 '17 at 20:23
  • This option will only work in the case you are creating a new file. If you are editing a previous file with comments out of a section, they will all wipe :/ – GEPD Aug 28 '17 at 00:35
9

Update for 3.7

I've been dealing with configparser lately and came across this post. Figured I'd update it with information relevant to 3.7.

Example 1:

config = configparser.ConfigParser(allow_no_value=True)
config.set('SECTION', '; This is a comment.', None)

Example 2:

config = configparser.ConfigParser(allow_no_value=True)
config['SECTION'] = {'; This is a comment':None, 'Option':'Value')

Example 3: If you want to keep your letter case unchanged (default is to convert all option:value pairs to lowercase)

config = configparser.ConfigParser(allow_no_value=True)
config.optionxform = str
config.set('SECTION', '; This Comment Will Keep Its Original Case', None)

Where "SECTION" is the case-sensitive section name you want the comment added to. Using "None" (no quotes) instead of an empty string ('') will allow you to set the comment without leaving a trailing "=".

dsanchez
  • 1,038
  • 9
  • 9
5

You could also use ConfigUpdater. It has many more convenience options to update configuration files in a minimal invasive way.

You would basically do:

from configupdater import ConfigUpdater

updater = ConfigUpdater()
updater.add_section('DEFAULT')
updater.set('DEFAULT', 'test', 1)
updater['DEFAULT']['test'].add_before.comment('test comment', comment_prefix=';')
with open('./config.ini', 'w') as f:
    updater.write(f)
fwilhelm
  • 348
  • 3
  • 11
5

You can create variable that starts by # or ; character:

conf.set('default_settings', '; comment here', '')
conf.set('default_settings', 'test', 1)

created conf file is

    [default_settings]
    ; comment here = 
    test = 1

ConfigParser.read function won't parse first value

config = ConfigParser.ConfigParser()
config.read('config.ini')
print config.items('default_settings')

gives

[('test','1')]
  • 2
    Pretty ugly with the trailing `=`. – ThiefMaster Jul 08 '11 at 07:18
  • Probably this is the best and only way with ConfigParser. Thanks – razvanc Jul 08 '11 at 07:42
  • You can get rid of the trailing `=`. When you want to write a comment you just omit the third argument when calling the `set` method. Otherwise you are assigning it an empty string which results in the unwanted trailing `=`. – benregn Sep 11 '11 at 15:38
  • If the set method doesn't receive a third parameter, then it will by default be `None`. so for example if we have `conf.set('DEFAULT', '; comment')`, we would get `; comment = None` – razvanc Nov 08 '11 at 14:55
1

Freaky solution for the above :) Note there is a side-effect, see if that suites you

config = configparser.ConfigParser(comment_prefixes='///')
config.set('section', '# cmt', 'comment goes here')

configparse will treat comments as variables, but real software would not. This would even preserve the comments on writes done after read of the same ini file, which is a real game changer (disappearing comments are just horrible) :) and you don't need to do allow_no_value=True to allow empty value, just minor visual candy :)

so the ini file would look like:

[section]
# cmt = comment goes here

which pretty much gets the job done :) please make sure to initialize comment_prefixes with a string that would never appear in your ini file just in case

This worked for me in 3.9.

Side effect on writing the already existing comments. They would not disappear which was normal default, but will be converted to a similar form # first = <remaining>, where first - first word of comment, remaining - remaining of the comment, which would change how file looks, so be carefull...

rokitokan
  • 91
  • 1
  • 1
  • 5