2012-05-27 55 views
3

我有一個python腳本需要執行位於另一個目錄中的.jar文件。什麼是最好的方法來做到這一點?到目前爲止,我在想 -如何執行需要在同一個目錄下的文件?

subprocess.call(["cd","/path/to/file"]) 
subprocess.call(["./file.jar"]) 

我應該怎麼做?

更新:

同時使用下面的答案,這是我落得這樣做:

subprocess.call(shlex.split("./file.jar -rest -of -command"), cwd=COMMAND_FOLDER) 

回答

7

要運行一個進程不同的當前工作目錄,使用subprocess.Popencwd參數:

import subprocess 
proc = subprocess.Popen(['file.jar'], cwd = '/path/to/file') 
2

howabout使用:

import subprocess 
import shlex 

cmd = "the command to use to execute your binary" 


args = shlex.split(cmd) 
try: 
    p = subprocess.call(args) 
except OSError, e: 
    print >>sys.stderr, "Execution failed:", e 
相關問題