2012-05-27 54 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 
相关问题