Java Property file parser and writer for Python

2.1.2 · active · verified Fri Apr 10

jProperties is a Python library for parsing and writing Java `.properties` files. It aims to provide similar functionality to Java's `java.util.Properties` class, supporting key-value pairs, comments, and line continuations. Version 2.1.2 is the latest stable release. The project is actively maintained on GitHub with a focus on robust handling of Java-style property files.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `.properties` file, load its contents into a `jproperties.Properties` object, access property values (including their associated metadata), modify and add new properties, and then store the updated properties back to a file. It highlights the use of `iso-8859-1` encoding, which is common for Java properties files.

import os
from jproperties import Properties

# 1. Define a .properties file content
props_content = '''\
# My Application Configuration
app.name=MyApp
app.version=1.0.0
app.env=development
# _some_meta=value
db.host=localhost
db.port=5432
'''

# 2. Write it to a temporary file
prop_file_path = "config.properties"
with open(prop_file_path, "wb") as f:
    # jproperties expects bytes, often iso-8859-1 for Java properties
    f.write(props_content.encode('iso-8859-1'))

# 3. Load the properties
p = Properties()
with open(prop_file_path, "rb") as f:
    p.load(f, "iso-8859-1") # Specify encoding, defaults to iso-8859-1

# 4. Access values
app_name, app_name_meta = p["app.name"]
print(f"App Name: {app_name} (Metadata: {app_name_meta})")

db_host, db_host_meta = p["db.host"]
print(f"DB Host: {db_host} (Metadata: {db_host_meta})")

# 5. Modify / Add properties (with metadata)
p["app.version"] = "1.1.0", {"last_updated": "2026-04-10"}
p["new.setting"] = "some_value"

# 6. Store properties to a new file (including metadata)
out_prop_file_path = "config_updated.properties"
with open(out_prop_file_path, "wb") as f:
    p.store(f, "Updated Configuration", timestamp=False, strip_meta=False)

print(f"\nUpdated configuration written to: {out_prop_file_path}")

# Clean up temporary files
os.remove(prop_file_path)
os.remove(out_prop_file_path)

view raw JSON →