加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 19:07:42 所属栏目:Linux 来源:网络整理
导读:subprocess.call(["/home/blah/trunk/blah/run.sh","/tmp/ad_xml","/tmp/video_xml"]) 我这样做但是,在我的run.sh中,我有“相对”路径. 所以,我必须“cd”到该目录,然后运行shell脚本.我怎么做? 最佳答案 使用cwd参数subprocess.call() 来自这里的文档:htt

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')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读