我有一个 python 脚本需要执行.jar位于另一个目录中的文件。最好的方法是什么?到目前为止,我在想——
subprocess.call(["cd","/path/to/file"])
subprocess.call(["./file.jar"])
我该怎么做?
更新:
使用下面的两个答案,这就是我最终要做的事情:
subprocess.call(shlex.split("./file.jar -rest -of -command"), cwd=COMMAND_FOLDER)
我有一个 python 脚本需要执行.jar位于另一个目录中的文件。最好的方法是什么?到目前为止,我在想——
subprocess.call(["cd","/path/to/file"])
subprocess.call(["./file.jar"])
我该怎么做?
更新:
使用下面的两个答案,这就是我最终要做的事情:
subprocess.call(shlex.split("./file.jar -rest -of -command"), cwd=COMMAND_FOLDER)
要在不同的当前工作目录中运行进程,请使用subprocess.Popen'scwd参数:
import subprocess
proc = subprocess.Popen(['file.jar'], cwd = '/path/to/file')
如何使用:
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