0

I'm trying to open an .exe file from Python and give it some instructions. This .exe file has its own "language" so, for example, in order to launch a model I should type "call ". Because I have thousands models to run I need to automatize the process.

Here on Stackoverflow I found several options that I tried. Even though I don't get any error, my .exe file doesn't run (actually a window opens and closes immediately). I'm writing one of these solutions. [I'm using 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

From this simple code I want "just" 'type' in an automatic way in my program "call .m", but it doesn't work.

Can anyone help me? Thanks

5
  • Depending on the executable it may require a real console input and can't handle a pipe. Commented Nov 8, 2017 at 12:05
  • When you run this program (opening directly the .exe file) a console opens. In this console you write the instructions (for example call ...) for running a model. So, if I understood, are you saying that I cannot use this method? Commented Nov 8, 2017 at 12:12
  • Maybe. You should read the documentation of the program or ask the programmer/producer. Commented Nov 8, 2017 at 12:23
  • when you launch some exe files, some of them check their parents_laucher! and if that parent_launcher ( in this case, your python script ) is not cmd or terminal or other systemic apps, they will not show their output to you! or give that current output to your script! becuase of copyright or other policies! capture your parent_laucher is not so hard! Commented Nov 8, 2017 at 15:21
  • did you check that? Commented Nov 8, 2017 at 15:22

1 Answer 1

1

Try This:

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

Another Way :

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

Third Way ( On Windows, install pywin32 package ):

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

Fourth way :

Save your commands in a .bat file like this:

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

Then try to launch this bat file and get its output:

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

Good Luck ...

Sign up to request clarification or add additional context in comments.

5 Comments

Hi DRPK, thanks so much for your prompt reply. Unfortunately they don't work. I should deeply have a look at that program, before trying again. Probably the problem is in it. Thanks anyway
@soligor: your welcome :) so may you vote up me? for other persons who wants this answer.
@soligor: when you launch some exe files, some of them check their parents_laucher! and if that parent_launcher ( in this case, your python script ) is not cmd or terminal or other systemic apps, they will not show their output to you! or give that current output to your script! becuase of copyright or other policies! capture your parent_laucher is not so hard!
I tried to do the same in Matlab. This is the script: 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 < call_model.txt &'); It works fine.
@soligor: maybe matlab call your exe file with cmd! ( matlab launch cmd and give your file_address to cmd )... with my upper codes try to luanch cmd and give your inputs...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.