0

Problem statement
What I want to do is run a program with a CLI. this program accepts one command at a time and execute it.(lets say cmd.exe) Most the time these commands goes in batches. So feeding a list of command at ones on one stream is going to work great but I also want to intervene manually time to time by sending some additional commands through the keyboard.

enter image description here

Initial attempt
I tried using CREATE_NEW_CONSOLE so that a new command window pops up as below.

from subprocess import Popen, PIPE, CREATE_NEW_CONSOLE
x = Popen('cmd.exe', creationflags=CREATE_NEW_CONSOLE)

So this allows me to feed commands manually and that part is ok. Now the question is how can I feed the same program some commands through x

My attempts to resolve it

  1. I tried giving stdin=PIPE but in that case python does not create a new console.
  2. I tried x.communicate(input='dir') for example but nothing appeared on the console or python prompt.
    enter image description here
  3. Tried to use os.mkfifo but this is depreciated.

Question
Is there a way to get this done using subprocess or some other means(possibly tempfile?). I am not interested on taking the output from the process for now.

XYZ
  • 310
  • 2
  • 12
  • You're expected to do research before asking here. For example, [An Introduction to Python Subprocess: Basics and Examples](https://www.datacamp.com/tutorial/python-subprocess) answers your question. The short of it is that you'll need to take control of input and output using pipes. Because of this, you won't be able to see that in the console unless your script prints that itself. In other words, your script will act as an interface between the user and the process. (Also `cmd` is likely a poor choice for testing this given that it's technically a GUI application.) – Ouroborus Aug 16 '23 at 04:32

0 Answers0