繁体   English   中英

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

[英]how to run an exe file with the arguments using python

假设我有一个文件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])

但它没有用。

如果需要,您也可以使用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的一种方便形式。 您可以在这个问题上阅读更多内容。

对于其他发现此问题的人,您现在可以使用subprocess.run() 这是一个例子:

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

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

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

应该管用。

在这里,我想提供一个很好的例子。 在下文中,我得到了当前程序的参数count ,然后将它们作为argProgram = []附加到数组中。 最后我调用了subprocess.call(argProgram)来完全直接地传递它们:

import subprocess
import sys

argProgram = []

if __name__ == "__main__":

    # Get arguments from input
    argCount = len(sys.argv)
    
    # Parse arguments
    for i in range(1, argCount):
        argProgram.append(sys.argv[i])

    # Finally run the prepared command
    subprocess.call(argProgram)

在这段code中,我应该运行一个名为“Bit7z.exe”的可执行应用程序:

 python Bit7zt.py Bit7zt.exe -e 1.zip -o extract_folder

注意:我使用for i in range(1, argCount):语句,因为我不需要第一个参数。

我不明白论点是如何起作用的。 例如:“-fps 30”不是一个,而是两个必须像这样传递的参数(Py3)

args=[exe,"-fps","30"].

也许这对某人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM