如何执行需要在同一目录中的文件?

时间:2012-05-27 20:15:26

标签: python

我有一个python脚本需要执行位于另一个目录中的.jar文件。最好的方法是什么?到目前为止,我在想 -

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

我该怎么做?

更新

使用以下两个答案,这就是我最终做的事情:

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

2 个答案:

答案 0 :(得分:7)

要在其他当前工作目录中运行流程,请使用subprocess.Popen的{​​{1}}参数:

cwd

答案 1 :(得分:2)

如何使用:

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