2011-01-29 41 views
5
subprocess.call(["/home/blah/trunk/blah/run.sh", "/tmp/ad_xml", "/tmp/video_xml"]) 

我这样做。但是,在我的run.sh中,我有“相对”路径。 因此,我必须“cd”进入该目录,然后运行shell脚本。我怎么做?如何在Python中运行bash脚本,但是像是从另一个目录运行一样?

+0

我不是一个子进程的专家,但你可以这样做:subprocess.call([“” CD /运行/路径; /home/blah/trunk/blah/run.sh“,”/ tmp/ad_xml“,”/ tmp/video_xml“])?? – inspectorG4dget 2011-01-29 02:51:03

回答

12

使用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/ad_xml", "/tmp/video_xml"], cwd='/tmp') 
+0

因此,我应该在该行的上面有一个subprocess.call? – TIMEX 2011-01-29 02:51:06

1

嗯,你可以使用subprocess.Popen与壳牌= True和CWD =“你想要的工作目录”

编辑:看来,呼叫具有相同的参数,以便只设置一个CWD论证会的工作:

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

您可以提供您的工作目录是这样的:

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

相关问题