1

I'm trying to set up an environment variable via Python:

os.environ["myRoot"]="/home/myName"
os.environ["subDir"]="$myRoot/subDir"

I expect the subDir environment variable to hold /home/myname/subDir, however it holds the string '$myRoot/subDir'. How do I get this functionality?

(Bigger picture : I'm reading a json file of environment variables and the ones lower down reference the ones higher up)

3 Answers 3

2

Use os.environ to fetch the value, and os.path to correctly put slashes in the right places:

os.environ["myRoot"]="/home/myName" 
os.environ["subDir"] = os.path.join(os.environ['myRoot'], "subDir")
Sign up to request clarification or add additional context in comments.

Comments

1

You can use os.path.expandvars to expand environment variables like so:

>>> import os
>>> print os.path.expandvars("My home directory is $HOME")
My home director is /home/Majaha
>>> 

For your example, you might do:

os.environ["myRoot"] = "/home/myName"
os.environ["subDir"] = os.path.expandvars("$myRoot/subDir")

I think @johntellsall's answer is the better for the specific example you gave, however I don't doubt you'll find this useful for your json work.

Edit: I would now recommend using @johntellsall's answer, as os.path.expandvars() is designed explicitly for use with paths, so using it for arbitrary strings may work but is kinda hacky.

Comments

0
def fix_text(txt,data):
    '''txt is the string to fix, data is the dictionary with the variable names/values'''
    def fixer(m): #takes a regex match 
         match = m.groups()[0] #since theres only one thats all we worry about
         #return a replacement or the variable name if its not in the dictionary
         return data.get(match,"$%s"%match) 
    return re.sub("$([a-zA-Z]+)",fixer,txt) #regular expression to match a "$" followed by 1 or more letters

with open("some.json") as f: #open the json file to read
    file_text= f.read()
data = json.loads(file_text) #load it into a json object
#try to ensure you evaluate them in the order you found them
keys = sorted(data.keys() ,key=file_text.index) 
#create a new dictionary by mapping our ordered keys above to "fixed" strings that support simple variables
data2= dict(map(lambda k:(k,fixer(data[k],data)),keys)
#sanity check
print data2

[edited to fix a typo that would cause it not to work]

3 Comments

I think a good explanation is required, I am guessing the OP is not overly experienced and using with to open the file or at least closing it would be better too ;)
meh I suppose you are right .... good practice and all ... still the file is opened and read from and then garbage collected ... with and close for reading files is not very important(but probably still a good practice for a new coder) ...
yep, not so essential in this question but definitely using with can help avoid some headaches later.

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.