Questions tagged [configparser]

ConfigParser is a Python module for reading and writing configuration files that have a structure similar to those of INI files in Microsoft Windows.

683 questions
238
votes
19 answers

Lists in ConfigParser

The typical ConfigParser generated file looks like: [Section] bar=foo [Section 2] bar2= baz Now, is there a way to index lists like, for instance: [Section 3] barList={ item1, item2 } Related question: Python’s ConfigParser unique keys per…
pistacchio
  • 56,889
  • 107
  • 278
  • 420
155
votes
27 answers

Properties file in python (similar to Java Properties)

Given the following format (.properties or .ini): propertyName1=propertyValue1 propertyName2=propertyValue2 ... propertyNameN=propertyValueN For Java there is the Properties class that offers functionality to parse / interact with the above…
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
135
votes
5 answers

Python extending with - using super() Python 3 vs Python 2

Originally I wanted to ask this question, but then I found it was already thought of before... Googling around I found this example of extending configparser. The following works with Python 3: $ python3 Python 3.2.3rc2 (default, Mar 21 2012,…
oz123
  • 27,559
  • 27
  • 125
  • 187
111
votes
5 answers

Preserve case in ConfigParser?

I have tried to use Python's ConfigParser module to save settings. For my app it's important that I preserve the case of each name in my sections. The docs mention that passing str() to ConfigParser.optionxform() would accomplish this, but it…
pojo
  • 5,892
  • 9
  • 35
  • 47
77
votes
7 answers

Where to put a configuration file in Python?

In development mode, I have the following directory tree : | my_project/ | setup.py | my_project/ | __init__.py | main.py | conf/ | myproject.conf I use ConfigParser to parse the myproject.conf file. In my code, it's easy to…
Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
64
votes
2 answers

Python config parser to get all the values from a section?

I want to get all the values from a section using config parser I used this but it gives only the first value def ConfigSectionMap(section): dict1 = {} options = Config.options(section) for option in options: try: dict1[option] =…
Rishabh
  • 3,752
  • 4
  • 47
  • 74
43
votes
5 answers

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…
razvanc
  • 930
  • 2
  • 11
  • 18
41
votes
4 answers

ConfigParser vs. import config

ConfigParser is the much debated vanilla configuration parser for Python. However you can simply import config where config.py has python code which sets configuration parameters. What are the pros\cons of these two approaches of configuration? When…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
38
votes
5 answers

MissingSectionHeaderError: File contains no section headers

I am trying to build collective.simserver according to this manual, with some modifications: instead of: virtualenv --python=bin/python2.7 simserver/ I am using: virtualenv --python=myVirtualEnv/bin/python simserver and I managed to come to this…
Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113
37
votes
3 answers

Booleans in ConfigParser always return True

This is my example script: import ConfigParser config = ConfigParser.ConfigParser() config.read('conf.ini') print bool(config.get('main', 'some_boolean')) print bool(config.get('main', 'some_other_boolean')) And this is…
user1447941
  • 3,675
  • 10
  • 29
  • 34
32
votes
1 answer

Python ConfigParser Checking existence of both Section and Key Value

Using ConfigParser's has_section() method I can check if a section exists in a file, such as: config.has_section(section_name) What would be a command to check if a key exists as well? So it would be possible to verify that both a section and key…
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
32
votes
1 answer

Python ConfigParser: Checking for option existence

I'm using Python's ConfigParser to create a configuration file. I want to check if a section has a particular option defined and, if it does, get the value. If the option isn't defined, I just want to continue without any special behavior. There…
user1272534
  • 991
  • 2
  • 11
  • 17
31
votes
3 answers

ConfigObj/ConfigParser vs. using YAML for Python settings file

Which is better for creating a settings file for Python programs, the built-in module (ConfigParser) or the independent project (ConfigObj), or using the YAML data serialization format? I have heard that ConfigObj is easier to use than…
Apophenia Overload
  • 2,485
  • 3
  • 28
  • 46
31
votes
2 answers

python ConfigParser read file doesn't exist

import ConfigParser config = ConfigParser.ConfigParser() config.read('test.ini') This is how we read a configuration file in Python. But what if the 'test.ini' file doesn't exist? Why doesn't this method throw an exception? How can I make it throw…
Cacheing
  • 3,431
  • 20
  • 46
  • 65
30
votes
3 answers

Iterate over sections in a config file

I recently got introduced to the library configparser. I would like to be able to check if each section has at least one Boolean value set to 1. For example: [Horizontal_Random_Readout_Size] Small_Readout = 0 Medium_Readout = 0 Large_Readout =…
Marmstrong
  • 1,686
  • 7
  • 30
  • 54
1
2 3
45 46