Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to read INI file in Python using ConfigParser

📅 2015-Feb-18 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

ConfigParser is a module that can be used to read and write INI files in Python. The module is straightforward to use. Here is a small example:

#!/usr/bin/env python

This module provides methods to read values from a INI file.

import ConfigParser

class Config(object):


    def __init__(self):
        self.cparser = None

    def read_config(self, config_path):
        self.cparser = ConfigParser.ConfigParser()

    def get_val(self, key):
        return self.cparser.get(Config.DEFAULT_SEC, key)

    def get_str(self, key):
        return str(self.get_val(key))

    def get_int(self, key):
        return int(self.get_val(key))

    def get_float(self, key):
        return float(self.get_val(key))

def main():
    print __doc__

if "__main__" == __name__:

Tried with: Python 2.7.6 and Ubuntu 14.04