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

问题描述 投票:13回答:3

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

RegressionSystem.exe -config filename

我尝试过:

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])

但它不起作用。

python windows python-2.7 subprocess
3个回答
17
投票

如果需要,您也可以使用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)

callPopen之间的差异基本上是call阻塞而Popen不是,Popen提供更多的一般功能。通常call适用于大多数用途,它基本上是一种方便的Popen形式。您可以在this question阅读更多内容。


3
投票
os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename")

应该管用。


2
投票

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

import subprocess
subprocess.run(["RegressionSystem.exe", "-config filename"])

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

© www.soinside.com 2019 - 2024. All rights reserved.