如何使用python运行带有参数的exe文件

bap*_*147 16 python windows subprocess python-2.7

假设我有一个文件RegressionSystem.exe.我想用-config参数执行这个可执行文件.命令行应该像:

RegressionSystem.exe -config filename
Run Code Online (Sandbox Code Playgroud)

我尝试过:

regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

hjw*_*ide 18

您也可以subprocess.call()根据需要使用.例如,

import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
Run Code Online (Sandbox Code Playgroud)

call和之间的区别Popen基本上call是阻塞,而Popen不是,Popen提供更一般的功能.通常call对大多数用途来说都很好,它本质上是一种方便的形式Popen.您可以在这个问题上阅读更多内容.


Dan*_*ler 14

接受的答案已过时。对于发现此问题的任何其他人,您现在可以使用subprocess.run(). 下面是一个例子:

import subprocess
subprocess.run(["RegressionSystem.exe", "-config filename"])
Run Code Online (Sandbox Code Playgroud)

参数也可以作为字符串发送,但您需要设置shell=True. 官方文档可以在这里找到。


Res*_*ted 3

os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename")
Run Code Online (Sandbox Code Playgroud)

应该管用。