The Wayback Machine - https://web.archive.org/web/20240118180630/https://www.geeksforgeeks.org/read-properties-file-using-jproperties-in-python/
Open In App
Related Articles

Read Properties File Using jproperties in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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 


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 03 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials