python problems with usingpath variable

时间:2019-04-08 12:45:46

标签: python-2.7 unix

There is a $CONFIG variable which stores the way to some dir on unix. I created following python script to test path variable in unix.

import os

path=os.environ.get('$CONFIG')

if os.path.exists(path) is True:
    print '{} path exists'.format(path)
else:
    print 'problem'

It gives me an error message:

Traceback (most recent call last):
 File "test.py", line 6, in <module>
    if os.path.exists(path) is True:
  File "/usr/lib64/python2.7/genericpath.py", line 18, in exists
    os.stat(path)

Woul you please help me how to correctly define path to unix folder using a variable in python script?

1 个答案:

答案 0 :(得分:1)

Remove the $ from the variable and it may work. ($ is used in linux terminal, not in python):

path=os.environ.get('CONFIG')