Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configparser.ParsingError: Source contains parsing errors: 'my.ini'

Tags:

python

I am getting this error: configparser.ParsingError: Source contains parsing errors: 'my.ini' although I am getting uncommented-values printed on the terminal.

my.ini:

[my]
# user
root
# passwd
password

I read here that # or ; could be used for commenting. This is how I am doing it:

import configparser

c = configparser.ConfigParser()
c.read('my.ini')
getval = c.items('my')
like image 401
lmao Avatar asked Aug 15 '18 18:08

lmao


People also ask

What is Configparser Configparser ()?

Source code: Lib/configparser.py. This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files.

What is Configparser module in Python?

The configparser module from Python's standard library defines functionality for reading and writing configuration files as used by Microsoft Windows OS. Such files usually have . INI extension. The INI file consists of sections, each led by a [section] header.

Is Configparser built in Python?

configparser comes from Python 3 and as such it works well with Unicode. The library is generally cleaned up in terms of internal data storage and reading/writing files.

How do I print a Configparser object?

Just use a StringIO object and the configparser's write method. It looks like the only method for "printing" the contents of a config object is ConfigParser. write which takes a file-like object.


1 Answers

The problem is root and password don't have a value assigned to them. Since it appears that you want to allow that, just say so when you create the ConfigParser instance:

c = configparser.ConfigParser(allow_no_value=True)

Or in Python 2:

c = ConfigParser.ConfigParser(allow_no_value=True)
like image 135
martineau Avatar answered Oct 13 '22 10:10

martineau