Start New Discussion within our Software Development Community

How to read properties file in Python? I found ConfigParser() but it has a 'section' limitation, so looking for other alternatives.



You could add a 'mock section' and still use ConfigParser

# tested with python 2.7.2
import ConfigParser
from functools import partial
from itertools import chain

class Helper:
    def __init__(self, section, file):
        self.readline = partial(next, chain(("[{0}]\n".format(section),), file, ("",)))

config = ConfigParser.RawConfigParser(allow_no_value=True)
with open("essconfig.cfg") as ifh:
    config.readfp(Helper("Foo", ifh))
print(config.get("Foo", "old_passwords"))

The config file was

user = mysql
pid-file = /var/run/mysqld/
old_passwords = 1

You are a lifesaver; it worked like a charm.

Thanks again,


sorry to bother you again but apparently, functools and itertools modules are not there in python 2.2.1. Can you give another version of your program that is compatible with 2.2.1?

This class should work for very old versions of python

class Helper:
    def __init__(self, section, file):
        self.section = "[%s]\n" % section
        self.file = file
        self.state = -1

    def readline(self):
        if self.state:
            if self.state < 0:
                self.state = 0
                return self.section
                return ""
            s = self.file.readline()
            if not s:
                self.state = 1
            return s

Why don't you upgrade ? Python 2.2 is a dinosaur :)

Thanks for your quick revert. Much as I'd like to upgrade, this python version is embedded in another product and I've to live with this :(

BTW, can you confirm if below code snippet would also work as it is with older python versions or it merits further changes? I'm getting invalid syntax error here, but seems to be misleading to me.

with open("essconfig.cfg") as ifh:
config.readfp(Helper("Foo", ifh))


    ifh = open("essconfig.cfg", "rb")
    config.readfp(Helper("Foo", ifh))

but I don't know how configparser was in python 2.2.1

Thank-You! seems to be working at first glance.

BTW, would you recommend using stringIO to achieve the same?

BTW, would you recommend using stringIO to achieve the same?

Given that configuration files are often small files, I would say why not ? Loading a small file in memory is not a very expensive operation.

I see you have get good help from Gribouillis.
As a note with open was new in python 2.5.
python 2.2.1 is really old.
Alternatives is to write your own parser that works for your files,it`s not so hard.
If i use config file postet by Grib.

cfg_dict = {}
lst = []
cfg_file = open('essconfig.cfg')
for line in cfg_file:
    if '-' and not '=' in  line:
        line = line.split()
        lst.append( line)
        line = ''.join(line.strip().split('=')).split()

for item in lst:
    cfg_dict[item[0]] = item[1]
print cfg_dict

Test it.

>>> cfg_dict['old_passwords']
>>> cfg_dict['pid-file']
>>> cfg_dict['skip-bdb']
>>> cfg_dict['user']

Dear Snippsat,

Thanks for responding. For now, my issue is resolvedb by using Gribouillis' suggestions; nevertheless, look forward to continued help from Python gurus on this forum.



This question has already been answered. Start a new discussion instead.