Through the python script, I launch an exe-file that is executed in the console and at the end of the work asks the user "Continue (y - yes, n - no)"

How can I enter the text through the python script already in the running console? for example, in my case, are the symbols y or n?

1 answer 1

Using subprocess.Popen , you can:

 from subprocess import Popen, PIPE process = Popen(['app.exe'], stdout=PIPE, stdin=PIPE, stderr=PIPE) stdout_data = process.communicate(input='y')[0] 

If you need to wait for the work to finish, and then write y / n, then before communicate add this cycle:

 while True: line = process.stdout.readline() if line == '': break 
  • If the process is waiting for input directly from the console, then the stdin=PIPE approach will not work. In trivia: 1- mention the version of Python (for example, the code as written will not work on Python 3 because of str vs. bytes difference) 2- it is not necessary to intercept the output (I do not see where it is indicated in the question) may never end if the process does not explicitly close its stdout, before waiting for input to stdin (probably not closing) 4- you can try: subprocess.run("app.exe", input=b"y") - jfs