I am trying to get my python app to start a command prompt session and run commands in it. I have been successful in getting it to start a cmd session, but I cannot make it run commands in the newly created window (nor any other open window). It only runs like in this picture:
Asked
Active
Viewed 414 times
1

GetTwoBirdsStonedAtOnce
- 313
- 3
- 11
-
Are you trying to run any commands? If so: can you explain what you are trying to achieve? – BramV Apr 25 '21 at 21:06
-
Yes, I am trying to start a command prompt session and then change the color using "color A/foo etc.". I am trying to make it run any other commands in the newly created window. – GetTwoBirdsStonedAtOnce Apr 25 '21 at 21:08
-
1do You know about `os.system("cmd command")`? which would mean that there is no need to launch cmd – Matiiss Apr 25 '21 at 21:14
-
I have tried it, but when I try to run any other command, the program commits suicide – GetTwoBirdsStonedAtOnce Apr 25 '21 at 21:36
1 Answers
1
I am trying to get my python app to start a command prompt session and run commands in it.
If I understand you correct, you're trying to send commands to a shell like it was typed on the keyboard.
To achieve that, you need
- start the shell process
- determine input and output streams of the shell process
- write and read some data to that streams
I'm not familiar with CMD shell and Windows, but here's the example how it could be done on linux with ZSH:
In [48]: import subprocess, time
...:
...:
...: def main():
...: with subprocess.Popen(['/usr/bin/zsh'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1) as proc:
...: proc.encoding = 'utf-8'
...: proc.text_mode = True
...: proc.universal_newlines = True
...: COMMAND = 'echo "Hello From Python! Current shell is \"$SHELL\"!"\r\n'
...:
...: print('Sending command:', proc.pid, '\n<<<', repr(COMMAND))
...:
...: proc.stdin.writelines([COMMAND.encode()])
...: proc.stdin.flush()
...:
...: print('Receiving outputs:', proc.pid, '\n>>>', end=' ')
...: raw = proc.stdout.readline()
...: res = raw.decode()
...: print(repr(res))
...:
...: proc.terminate()
...:
...: main()
Sending command: 1250722
<<< 'echo "Hello From Python! Current shell is "$SHELL"!"\r\n'
Receiving outputs: 1250722
>>> 'Hello From Python! Current shell is /usr/bin/zsh!\r\n'
If you need to go deeper, please take a look at this answer: Understanding Popen.communicate

madbird
- 1,326
- 7
- 11
-
Hi, I got most of it to work, using the solution I found here: https://stackoverflow.com/questions/39716557/how-to-use-subprocess-popen-with-built-in-command-on-windows#39717215 It shows output but it doesn't allow the user to type anything and I want to allow that. Edit: It has to do with the fact that I am using a kivy app so the problem resides there. Thanks for your help! – GetTwoBirdsStonedAtOnce Apr 26 '21 at 08:59
-
Oh, I got it. @GetTwoBirdsStonedAtOnce, if you need to start cmd with initial setup, and setup script is not seems to be heavily dependent on python variables, I could suggest using BAT file for starting CMD and setting up colors there, rather than doing that from python though. And to bind the started process directly to a parent's terminal, just remove `stdin=PIPE` and `stdout=PIPE`. – madbird Apr 26 '21 at 09:18
-
I will try it, but the thing I am trying to accomplish is to let the user write in the cmd and if necessary click on a button in the app and make it run the command that is bound to the button in the window that user is writing in. Hope I explained it clearly. – GetTwoBirdsStonedAtOnce Apr 26 '21 at 09:39