Open In App

Read Properties File Using jproperties in Python

Last Updated : 03 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to read properties file in python using jproperties module. It is a Java Property file parser and writer for Python. For installation run this command into your terminal.

pip install jproperties

Various properties of this module:

  • get() Method or Index-Based access to reading the values associated with the key.
  • items() method to get the collection of all key-value pairs and iterate over it to read all keys — value pair from the properties.
  • The file contains key-value pairs in each line (i.e it is a dictionary in python). This operator equals (=) works as a delimiter between the key and value

We will use this properties file(example.properties) for demonstration:

Example 1: Printing all properties details.

Approach:

  • Import the module
  • Load the properties file into our properties object.
  • Here items() method is to get the collection of the tuple, Which contains Keys and Corresponding PropertyTuple values

Below is the implementation:

Python3




from jproperties import Properties
  
configs = Properties()
with open('example.properties', 'rb') as read_prop:
    configs.load(read_prop)
      
prop_view = configs.items()
print(type(prop_view))
   
for item in prop_view:
    print(item)


Output:

Output Of The  Program Using items() Method 

Example 2: Printing properties file In the basis of key, values pairs like in dictionary in Python 

Approach:

  • Import the module
  • Then we open the .properties file in ‘rb’ mode then we use load() function
  • Then we use items() method to get the collection all key-value pairs here (i.e: print(type(prop_view)) prints the class type  of the argument specified

Below is the full implementation:

Python3




from jproperties import Properties
configs = Properties()
  
with open('example.properties', 'rb') as read_prop:
    configs.load(read_prop)
      
prop_view = configs.items()
print(type(prop_view))
   
for item in prop_view:
    print(item[0], '=', item[1].data)


Output:

Example 3: Printing each specific values-data as we need 

Approach:

  • Import the module
  • Then we open the .properties file in ‘rb’ mode then we use load() function
  • Use the get() method to return the value of the item with the specified key.
  • Use len() function to get the count of properties of the file.

Below is the full implementation:

Python3




from jproperties import Properties
configs = Properties()
  
with open('example.properties', 'rb') as read_prop:
    configs.load(read_prop)
      
print(configs.get("DB_User"))  
print(f'Database User: {configs.get("DB_User").data}')   
print(f'Database Password: {configs["DB_PWD"].data}')  
print(f'Properties Count: {len(configs)}')   


Output:

Here Is The Output of  Properties File Using get() Method 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads