How can I execute a program from within python program so that it opens in a separate cmd.exe window with the output of the executed program? I tried using subprocess.popen but it doesn't display the cmd.exe window while the program is running.
Asked
Active
Viewed 7,611 times
1 Answers
6
In Windows, you need to declare optional variable shell=True and use start:
subprocess.Popen('start executable.exe', shell=True)
or if you want to kill the shell after running the executable:
subprocess.Popen('start cmd /C executable.exe', shell=True)
For example:
subprocess.Popen('start dir', shell=True)
subprocess.Popen('start cmd /C dir', shell=True)

mpaf
- 6,597
- 6
- 38
- 42
-
I tried import subprocess, os cd=os.getcwd() p = subprocess.Popen([cd+"\\"+'cmd.exe',cd+"\\"+'program.bat'], shell=True) but it doesnt open in cmd.exe window. – Sunny88 Dec 09 '13 at 12:32
-
try adding start before your executable – mpaf Dec 09 '13 at 12:35
-
1The problem with using `start` is that is spawns a new process and does not wait for it to finish. I.e., the returned Popen object's `wait()` method returns immediately and there is no direct way to know when the process finishes. If you want to start the process in the new console and still be able to correctly wait for process to finish, use Popen normally (no 'start', 'cmd', shell=True), but just pass the additional `creationflags=subprocess.CREATE_NEW_CONSOLE` parameter). Details: https://stackoverflow.com/a/20612529/23715 – Alex Che Apr 10 '18 at 09:48