Sourcing properties files with Python


Tags:

I've been looking for a good way to have a python script source a .properties file for environment variables and I found a few posts that pointed to ConfigParser and it works perfectly. Here is the sample script:

------------------------------------------------
from ConfigParser import ConfigParser

properties = "test.properties"

""" process properties file """
cfg = ConfigParser()
cfg.read(properties)
JAVA_HOME = cfg.get("config", "JAVA_HOME")
DOMAINAME = cfg.get("config", "DOMAINAME")

print JAVA_HOME 
print DOMAINAME 
------------------------------------------------

and here is 'test.properties'

------------------------------------------------
[config]
JAVA_HOME="/usr/jdk/bin"
DOMAINAME="crorgdomain"
------------------------------------------------

...seems pretty simple.