开发者

PATH variable in Python

开发者 https://www.devze.com 2023-04-08 17:46 出处:网络
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 anot

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

精彩评论

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