1

I have a .properties file which stores some keys and values that I would like to use it from my python file.

My test.properties file is something like that:

attribute1=username
attribute2=address
attribute3=class

I would like to access these attributes from a python file such that when I do something like:

attribute1 = "tom123"
attribute2 = "5 Smith Street"
attribute3 = "402"

But right now I am wondering how to import the config.properties file in python and start using the properties defined. Any help will be appreciated thanks.

1 Answer 1

1

You can load your config file in a python dictionary like this:

config = {}

with open('config.properties', 'r', ) as f:
    for line in f.readlines():
        line = line.strip()  # removes the newline characters
        parts = line.split("=")  # creates a (key, value) tuple
        key = parts[0]
        value = parts[1]
        config[key] = value

attribute1 = config['attribute1']
attribute2 = config['attribute2']
attribute3 = config['attribute3']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.