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

时间:2021-09-14 01:28:23
subprocess.call(["/home/blah/trunk/blah/run.sh", "/tmp/ad_xml", "/tmp/video_xml"])

I do this. However, inside my run.sh, I have "relative" paths. So, I have to "cd" into that directory, and then run the shell script. How do I do that?

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

4 个解决方案

#1


12  

Use the cwd argument to subprocess.call()

使用cwd参数subprocess.call()

From the docs here: http://docs.python.org/library/subprocess.html

来自这里的文档: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.

如果cwd不是None,则子节点的当前目录在执行之前将更改为cwd。请注意,在搜索可执行文件时不考虑此目录,因此您无法指定程序相对于cwd的路径。

Example:

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

#2


1  

Well, you could use subprocess.Popen with Shell = True and cwd = "Your desired working directory"

好吧,你可以使用带有Shell = True的subprocess.Popen和cwd =“你想要的工作目录”

EDIT: It appears that call has the same arguments so just setting a cwd argument would work:

编辑:似乎调用具有相同的参数,所以只设置一个cwd参数将起作用:

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

#3


1  

You can supply your working directory like this:

你可以像这样提供你的工作目录:

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

subprocess.call([“/ home / blah / trunk / blah / run.sh”,“/ tmp / ad_xml”,“/ tmp / video_xml”],cwd =“/ home / blah / trunk / blah”)

#1


12  

Use the cwd argument to subprocess.call()

使用cwd参数subprocess.call()

From the docs here: http://docs.python.org/library/subprocess.html

来自这里的文档: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.

如果cwd不是None,则子节点的当前目录在执行之前将更改为cwd。请注意,在搜索可执行文件时不考虑此目录,因此您无法指定程序相对于cwd的路径。

Example:

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

#2


1  

Well, you could use subprocess.Popen with Shell = True and cwd = "Your desired working directory"

好吧,你可以使用带有Shell = True的subprocess.Popen和cwd =“你想要的工作目录”

EDIT: It appears that call has the same arguments so just setting a cwd argument would work:

编辑:似乎调用具有相同的参数,所以只设置一个cwd参数将起作用:

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

#3


1  

You can supply your working directory like this:

你可以像这样提供你的工作目录:

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

subprocess.call([“/ home / blah / trunk / blah / run.sh”,“/ tmp / ad_xml”,“/ tmp / video_xml”],cwd =“/ home / blah / trunk / blah”)

#4