Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigParser - print config.sections() returns []

I'm trying to use a ConfigParser module to parse an *.ini file. The problem is that when I try to print sections or whatever, it returns empty list [].

config.ini

[SERVER]
host=localhost
port=9999
max_clients=5
[REGULAR_EXPRESSIONS]
regular_expressions_file_path=commands/commands_dict

config.py

# -*- coding: utf-8 -*- 
import ConfigParser

config = ConfigParser.SafeConfigParser()
config.read("config.ini")
print config.sections()

[]

Do you know where is the problem?

EDIT: Here is a screen of my structure:enter image description here

like image 355
Milano Avatar asked Sep 15 '26 20:09

Milano


1 Answers

Your code works for me. Are you sure that your CWD points to the right directory with the right config.ini file in it?

$ cat config.ini
[SERVER]
host=localhost
port=9999
max_clients=5
[REGULAR_EXPRESSIONS]
regular_expressions_file_path=commands/commands_dict

$ python2.7
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ConfigParser
>>> cp = ConfigParser.SafeConfigParser()
>>> cp.read('config.ini')
['config.ini']
>>> cp.sections()
['SERVER', 'REGULAR_EXPRESSIONS']
>>> ^D
like image 130
aghast Avatar answered Sep 18 '26 09:09

aghast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!