如何在Python中运行bash脚本,但就好像它从另一个目录运行?

前端之家收集整理的这篇文章主要介绍了如何在Python中运行bash脚本,但就好像它从另一个目录运行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

subprocess.call(["/home/blah/trunk/blah/run.sh","/tmp/ad_xml","/tmp/video_xml"])

我这样做但是,在我的run.sh中,我有“相对”路径.
所以,我必须“cd”到该目录,然后运行shell脚本.我怎么做?

最佳答案
使用cwd参数subprocess.call()

来自这里的文档:http://docs.python.org/library/subprocess.html

If cwd is not None,the child’s
current directory will be changed to
cwd before it is executed. Note that
this directory is not considered when
searching the executable,so you can’t
specify the program’s path relative to
cwd.

例:

subprocess.call(["/home/blah/trunk/blah/run.sh","/tmp/video_xml"],cwd='/tmp')
原文链接:https://www.f2er.com/linux/440415.html

猜你在找的Linux相关文章