How to read properties file in Python using configparser?

In this tutorial we are going to write Python program that will read the properties files using the configparser library of Python.

How to read properties file in Python using configparser?

How to read properties file in Python using configparser and printing the property value?

In Python we can read the configuration file and then is required when you have to use the properties written in properties file in your program. In this tutorial we are going to make a sample property file and then read the values inside this file using configparser library of Python. The configparser library is a Python library which is used to read the configuration files in Python and you can use this to read the configuration files in your program. For example you can store the database connection details in properties file and then use the configparser to parse and read the database connection credentials.

The main use of properties file in programming is to store the configuration parameters, which can be used by program during runtime. For example you can define the database connection string, host, username and password in the properties file and then use these values to connect to database during program execution. In future if your database parameters are changed then you just have to update the values in the property file while your program will remain unchanged. So, this way programmers are using properties file in their program to save various configurable parameters of the application.

The configparser library comes with Python 3 and it can be imported in Python program by using the import statement 'import configparser'. After import this library in your program you can create the instance of the parser library and then use it for parsing the properties file.  After reading the properties file you can get the value of your property with the help of get() method. Following example shows you how to read the properties file in Python very easily.

Sample Property file

First of all we will create a sample property file and add the properties. Here is sample example of properties file:

# Database Configuration

[db]
db_url=jdbc:mysql://localhost:3308/mydb
user=root
password=mypass

Here is the screen  shot of the properties file:

Properties file

 

Installing configparser

The configparser is python library and you should first install in your python environment. The command to install configparser library is:

pip install configparser

Above command will install configparser  library in you Python environment. Now we can proceed for developing the program to read properties file using configparser library.

Reading properties file in Python using configparser

First of all you have to import the configparser library in your program and for this you can use following python code:


import configparser

Then we will be able to use this library to load parse the .properties file. After reading the db.properties we can use the get() method to get the value of property in our Python program.

Here is the complete code to read properties file in Python using the configparser:


import configparser

config = configparser.ConfigParser()
config.read('db.properties')

db_url=config.get("db", "db_url")
user=config.get("db", "user")
password=config.get("db", "password")

print(db_url)
print(user)
print(password)

Here is the screen shot of the python program in IDE:

Read Properites file in Python 

You can run this program by following command:


python readprop.py

Above program will read the property file and print the values of property db_url, user and password on the console.

Here is the output of the program:

$ python readprop.py 
jdbc:mysql://localhost:3308/mydb
root
mypass

This way you will be able to use the configparser in Python to read the properties file.

Check more tutorials at: