开发者

PATH variable in Python

How can I change the Environmental PATH 开发者_C百科variable through code in python ? I am trying to return the path of an executable file. The code does not work because the shell is pointing to another directory. Any help is appreciated.


You can use os.environ.

Example:

path = os.environ["PATH"] # a ':'-separated string
path += ":/var/custom/bin"
os.environ["PATH"] = path

Or in a single line:

os.environ["PATH"] = ':'.join(os.environ["PATH"].split(":") + ["/var/bin"])


You aren't looking for the PATH variable. You want to either set the current working directory with os.chdir or pass the absolute path with os.path.abspath.


os.environ["PATH"] += ":/usr/local/bin"

See http://docs.python.org/library/os.html#os.environ.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜