【问题标题】:Open an .exe file and give it input parameters in Python打开一个 .exe 文件并在 Python 中为其提供输入参数
【发布时间】:2017-11-08 11:59:34
【问题描述】:

我正在尝试从 Python 打开一个 .exe 文件并给出一些说明。这个 .exe 文件有它自己的“语言”,例如,为了启动一个模型,我应该输入“call”。因为我有数千个模型要运行,所以我需要自动化这个过程。

在 Stackoverflow 上,我找到了几个我尝试过的选项。即使我没有收到任何错误,我的 .exe 文件也没有运行(实际上是一个窗口立即打开和关闭)。我正在写这些解决方案之一。 [我正在使用 Python3]:

from subprocess import Popen, PIPE

p = Popen(["my_file.exe"], stdin=PIPE, stdout=PIPE)  
output = p.communicate(input=b"call test.m")      # "call test.m" is the way to run a model in my_file.exe

从这个简单的代码中,我想在我的程序“call .m”中自动“输入”“类型”,但它不起作用。

谁能帮助我? 谢谢

【问题讨论】:

  • 取决于可执行文件,它可能需要真正的控制台输入并且无法处理管道。
  • 当您运行该程序(直接打开 .exe 文件)时,会打开一个控制台。在此控制台中,您编写运行模型的指令(例如调用 ...)。那么,如果我理解了,你是说我不能使用这种方法吗?
  • 也许吧。您应该阅读程序的文档或询问程序员/制作人。
  • 当你启动一些exe文件时,其中一些会检查它们的parents_laucher!如果那个 parent_launcher (在这种情况下,你的 python 脚本)不是 cmd 或终端或其他系统应用程序,它们将不会向你显示它们的输出!或将当前输出提供给您的脚本!因为版权或其他政策!捕获你的 parent_laucher 并不难!
  • 你检查了吗?

标签: python file input external


【解决方案1】:

试试这个:

from subprocess import Popen, check_output, check_call, PIPE, call


get_input = input("What Should I do?")

if get_input.strip().lower() == "run":

    your_exe_file_address = r'"C:\Users\you\Desktop\my_file.exe"' # example
    your_module_address = r'"C:\Users\you\Desktop\test.m"' # example
    your_command = "call"

    process = Popen([your_exe_file_address, your_command, your_module_address], stdout=PIPE, stderr=PIPE, shell=True)
    stdout, stderr = process.communicate()

    # < Other Ways >
    # process = check_output([your_exe_file_address, your_command, your_module_address])
    # process = check_call([your_exe_file_address, your_command, your_module_address], shell=True)
    # process = call([your_exe_file_address, your_command, your_module_address], stdout=PIPE, stderr=PIPE, shell=True)

    print(stdout, stderr)

else:
    print("Invalid Input")

另一种方式:

import os

get_input = input("What Should I do?")

if get_input.strip().lower() == "run":

    your_exe_file_address = r'"C:\Users\you\Desktop\my_file.exe"' # example
    your_module_address = r'"C:\Users\you\Desktop\test.m"' # example
    your_command = "call"

    last_shell = your_exe_file_address + " " + your_command + " " + your_module_address
    os.system(last_shell)

else:
    print("Invalid Input")

第三种方式(在 Windows 上,安装 pywin32 包):

import win32com.client

get_input = input("What Should I do?")

if get_input.strip().lower() == "run":

    your_exe_file_address = r'"C:\Users\you\Desktop\my_file.exe"' # example
    your_module_address = r'"C:\Users\you\Desktop\test.m"' # example
    your_command = "call"

    last_shell = your_exe_file_address + " " + your_command + " " + your_module_address
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.Run(last_shell)

else:
    print("Invalid Input")

第四种方式:

将您的命令保存在 .bat 文件中,如下所示:

"C:\Users\you\Desktop\my_file.exe" call "C:\Users\you\Desktop\test.m"

然后尝试启动这个 bat 文件并获取它的输出:

import os

get_input = input("What Should I do?")

if get_input.strip().lower() == "run":

    your_bat_file_address = r'"C:\Users\you\Desktop\my_bat.bat"' # example
    os.startfile(your_bat_file_address)

else:
    print("Invalid Input")

祝你好运...

【讨论】:

  • 您好 DRPK,非常感谢您的及时回复。不幸的是,它们不起作用。在重试之前,我应该深入了解该程序。可能问题就在其中。还是谢谢
  • @soligor:欢迎您 :) 那么您可以投票给我吗?对于其他想要这个答案的人。
  • @soligor:当你启动一些exe文件时,其中一些会检查它们的parents_laucher!如果那个 parent_launcher (在这种情况下,你的 python 脚本)不是 cmd 或终端或其他系统应用程序,它们将不会向你显示它们的输出!或将当前输出提供给您的脚本!因为版权或其他政策!捕获你的 parent_laucher 并不难!
  • 我尝试在 Matlab 中做同样的事情。这是脚本: header1 = 'call'; header2 = 'test.m' fid = fopen('call_model.txt','w'); fprintf(fid, [header1 ' ' header2]); fprintf(fid, '%f %f'); fclose(fid); system('my_file.exe
  • @soligor:也许matlab用cmd调用你的exe文件! ( matlab 启动 cmd 并将您的 file_address 提供给 cmd )...用我的上层代码尝试 luanch cmd 并提供您的输入...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多