2011-01-29 28 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")

相關問題