Writing comments to files with ConfigParser

Question:

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.

Asked By: razvanc

||

Answers:

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')]
Answered By: Marat Zaynutdinoff

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
Answered By: martin.preinfalk

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)
Answered By: fwilhelm

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 “=”.

Answered By: dsanchez

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…

Answered By: rokitokan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.