Reputation: 10302
I have an executable xyz.exe
that is purely driven by command prompt/shell. It interactively takes input and displays output. Assume xyz.exe
is somewhat like the typical python shell on windows. It waits for user to type something >>>
and thereby proceeds to process the input.
Now, I want to get control over this xyz.exe
process and bring it entirely into the context of my current python console/shell. How can I do this?
I referred Assign output of os.system to a variable and prevent it from being displayed on the screen and tried doing the same using subprocess:
import subprocess
cmd = r"\\sharedLocation\path\xyz.exe"
proc = subprocess.Popen([cmd], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print("program output:", out)
but that only gets the immediate (first time) output of xyz.exe
when it starts, more like the welcome message. But after the first time output/welcome message, the xyz.exe
actually waits for user input ">>>"
... I want my current execution of the python script to wait()
and bring-in the entire context of xyz.exe
inside the scope of my script output. Is there a way to do this in python?
Upvotes: 0
Views: 701
Reputation: 4079
Actually, whatever you input will be fed to xyz.exe
but you won't see any further messages as stdout=subprocess.PIPE
has been passed to Popen's constructor, which will make the output of the command not be outputted; instead it is fed to the file-like proc.stdout
. The welcome message is printed because of print("program output:", out)
. To make the output of xyz.exe
show up in the console remove stdout=subprocess.PIPE,
from the Popen constructor arguments. Then print("program output:", out)
is unnecessary.
Upvotes: 1