How to read a config file using python

In order to use my example, your file “abc.txt” needs to look like this.

[your-config]
path1 = "D:\test1\first"
path2 = "D:\test2\second"
path3 = "D:\test2\third"

Then in your code you can use the config parser.

import ConfigParser

configParser = ConfigParser.RawConfigParser()   
configFilePath = r'c:\abc.txt'
configParser.read(configFilePath)

As human.js noted in his comment, in Python 3, ConfigParser has been renamed configparser. See Python 3 ImportError: No module named ‘ConfigParser’ for more details.

Leave a Comment