I have an executable that I can run interactively from the windows command line. The workflow is as follows:
C:\Users\Me> my_executable.exe # Running the executable from CMD
Welcome! Please choose one:
0: Exit
1: Sub-task 1
2: Sub-task 2
Enter your input: 2 # I entered this interactively
Sub-task 2 chosen.
Please choose next option:
0: Return to previous menu
1: Connect to server
2: Disconnect from server
3: Call server API 1
4: Call server API 2
Enter your input: 1 # I entered this interactively
The full workflow is complex and takes multiple passes (once through sub-task 1, then through sub-task 2 etc).
I would like to automate this using Python. I have been trying to use the subprocess module to achieve this. However, I seem to fail at the .Popen stage itself.
Here is a similar question on StackOverflow. I tried using the same approach using .Popen. This is what I see:
C:\Users\Me> python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import Popen, PIPE
>>> p = Popen('my-executable.exe', stdin=PIPE)
Traceback (most recent call last):
file "<stdin>", line 1 in <module>
TypeError: unsupported operand type(s) for -:'Popen' and 'Popen'
>>> Welcome! Please choose one:
0: Exit
1: Sub-task 1
2: Sub-task 2
Enter your input: 2 # I am forced to enter this input here.
2 # Python echoes 2 before returning to prompt
>>> # Back to Python prompt
Can someone help with what I am doing wrong?