How to read a config file using python

Posted on

Question :

How to read a config file using python

I have a config file abc.txt which looks somewhat like:

path1 = "D:test1first"
path2 = "D:test2second"
path3 = "D:test2third"

I want to read these paths from the abc.txt to use it in my program to avoid hard coding.

Answer #1:

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

path1 = "D:test1first"
path2 = "D:test2second"
path3 = "D:test2third"

Then in your software you can use the config parser:

import ConfigParser

and then in you code:

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

Use case:

self.path = configParser.get('your-config', 'path1')

*Edit (@human.js)

in python 3, ConfigParser is renamed to configparser (as described here)

Answered By: Kobi K

Answer #2:

You need a section in your file:

[My Section]
path1 = D:test1first
path2 = D:test2second
path3 = D:test2third

Then, read the properties:

import ConfigParser

config = ConfigParser.ConfigParser()
path1 = config.get('My Section', 'path1')
path2 = config.get('My Section', 'path2')
path3 = config.get('My Section', 'path3')
Answered By: user278064

Answer #3:

If you need to read all values from a section in properties file in a simple manner:

Your config.cfg file layout :

key1 = value1  
key2 = value2  

You code:

   import configparser

   config = configparser.RawConfigParser()'path_to_config.cfg file')
   details_dict = dict(config.items('SECTION_NAME'))

This will give you a dictionary where keys are same as in config file and their corresponding values.

details_dict is :

{'key1':'value1', 'key2':'value2'}

Now to get key1’s value :

Putting it all in a method which reads sections from config file only once(the first time the method is called during a program run).

def get_config_dict():
    if not hasattr(get_config_dict, 'config_dict'):
        get_config_dict.config_dict = dict(config.items('SECTION_NAME'))
    return get_config_dict.config_dict

Now call the above function and get the required key’s value :

config_details = get_config_dict()
key_1_value = config_details['key1'] 

Generic Multi Section approach:

key1 = value1  
key2 = value2  

key1 = value1  
key2 = value2

Extending the approach mentioned above, reading section by section automatically and then accessing by section name followed by key name.

def get_config_section():
    if not hasattr(get_config_section, 'section_dict'):
        get_config_section.section_dict = collections.defaultdict()
        for section in config.sections():
            get_config_section.section_dict[section] = dict(config.items(section))
    return get_config_section.section_dict

To access:

config_dict = get_config_section()

port = config_dict['DB']['port'] 

(here ‘DB’ is a section name in config file
and ‘port’ is a key under section ‘DB’.)

Answered By: MANU

Answer #4:

This looks like valid Python code, so if the file is on your project’s classpath (and not in some other directory or in arbitrary places) one way would be just to rename the file to “” and import it as a module, using import abc. You can even update the values using the reload function later. Then access the values as abc.path1 etc.

Of course, this can be dangerous in case the file contains other code that will be executed. I would not use it in any real, professional project, but for a small script or in interactive mode this seems to be the simplest solution.

Just put the into the same directory as your script, or the directory where you open the interactive shell, and do import abc or from abc import *.

Answered By: tobias_k

Answer #5:

Since your config file is a normal text file, just read it using the open function:

file = open("abc.txt", 'r')
content =
paths = content.split("n") #split it into lines
for path in paths:
    print path.split(" = ")[1]

This will print your paths. You can also store them using dictionaries or lists.

path_list = []
path_dict = {}
for path in paths:
    p = path.split(" = ")
    path_dict[p[0]] = p[1]

More on reading/writing file here.
Hope this helps!

Answered By: aIKid

Answer #6:

A convenient solution in your case would be to include the configs in a yaml file named
**your_config_name.yml** which would look like this:

path1: "D:test1first"
path2: "D:test2second"
path3: "D:test2third"

In your python code you can then load the config params into a dictionary by doing this:

import yaml
with open('your_config_name.yml') as stream:
    config = yaml.safe_load(stream)

You then access e.g. path1 like this from your dictionary config:


To import yaml you first have to install the package as such: pip install pyyaml into your chosen virtual environment.

Answered By: kjoeter

Leave a Reply

Your email address will not be published. Required fields are marked *