Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing comments to files with ConfigParser

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.

like image 753
razvanc Avatar asked Jul 08 '11 06:07

razvanc


People also ask

How do you put comments in a config file?

To enter comments in a configuration file, use a comment character and enter the comment text anywhere to the right of the comment character on the same line. Valid comment characters are a semicolon (;), a pound sign (#), or two hyphens (--). You can start comments in any column of a separate line.

How do I comment in config file in Python?

From the docs: Lines beginning with '#' or ';' are ignored and may be used to provide comments. Configuration files may include comments, prefixed by specific characters (# and ;). Comments may appear on their own in an otherwise empty line, or may be entered in lines holding values or section names.

How do I use Configparser in Python?

Read and parse one configuration file, given as a file object. Read configuration from a given string. Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.

How do you write an INI file in Python?

To read and write INI files, we can use the configparser module. This module is a part of Python's standard library and is built for managing INI files found in Microsoft Windows. This module has a class ConfigParser containing all the utilities to play around with INI files.


2 Answers

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 
like image 183
martin.preinfalk Avatar answered Sep 19 '22 15:09

martin.preinfalk


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 "=".

like image 31
dsanchez Avatar answered Sep 22 '22 15:09

dsanchez