简体   繁体   中英

How to open a command line window to run another portable executable and then get output from there in Python?

So, I have an incredibly basic portable.exe file, written in C++:

#include <iostream>

using namespace std;

int main(){
    string user_input;
    cout << "Input here! ";
    getline(cin, user_input);
    system("cls");
    cout << user_input;
    return 0;
}

This displays "Input here! " and rewrites user_input onto the console after clearing it. Therefore, at the end, if polled, the console should only have user_input to report back.

After compiling it with MinGW as follows, running it on itself produces completely expected results:

g++ -Wall -fexceptions -O2 -o simple_program.exe -static-libgcc -static-libstdc++ simple_program.cpp

However, I am trying to somehow launch this program through a Python script in another console window, get it to run, and fetch the resulting information back to the Python script. From what I've gathered, this is to be done through the subprocess module, but I am unable to implement it correctly.

I tried to use the following, all with and without shell=True , stdin=subprocess.PIPE , stdout=subprocess.PIPE when applicable, however, the best I can get is a flashing of another console window and no actual execution.

import subprocess

subprocess.Popen(["simple_program.exe"]).communicate()[0]  # returns b''
subprocess.Popen(["cmd", "/c", "simple_program.exe"]).communicate()[0]  # returns b''
subprocess.Popen(["start", "/wait", "cmd", "/c", "simple_program.exe"]).communicate()[0]  # flashes console, returns b''
subprocess.getoutput("simple_program.exe")  # returns ''
subprocess.getoutput("cmd /c simple_program.exe")  # returns ''
subprocess.getoutput("start /wait cmd /c simple_program.exe")  # flashes console, returns ''

In the case of cmd /k , that only opens a new window but does not actually execute anything.

So, I turn to stackoverflow, to ask: How can I get this.exe to execute in another window and get that window's output into a Python variable?

Thanks.

Does this help?

It opens another window (I call it B) and display B output after window B is closed. I copied and pasted your cpp code,built D:/QtTestProg/Debug/QtWidgetsApplication1.exe

import subprocess
from subprocess import *

print(subprocess.Popen('D:/QtTestProg/Debug/QtWidgetsApplication1.exe', creationflags=CREATE_NEW_CONSOLE,stdout=PIPE,text='true').communicate(timeout=15)[0])


So I ended up not solving this problem, but circumventing it.

As I also had control over the C++ source code, I just dumped the output onto a temporary file, which the Python side can read and then discard of.

If you absolutely need to do this through memory, I see two ways:

  1. Rewriting part of the multiprocessing.Popen code to poll for stdout , but not actually capture it and reflect it onto the main console. The best way to do this, in my opinion, would be to debug the ending routine of multiprocessing.Popen to squeeze a polling call in somewhere and implement this through inheritence and a subclass. Although, I am unfamiliar with how stdout is implemented on a system level, and this may not be possible. This would require no tempering with the source code of the console application though, which may make this the only solution for working with proprietary code.

  2. Writing some sort of named pipe between the C++ and Python processes, and stuffing the data onto there. In my honest opinion though, if you are able to edit the source code of the application, it is not worth the effort to not use disk because memory is "better practice".

Anyways explorer, good luck out there!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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