Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Start new command prompt on Windows and wait for it finish/exit

Tags:

python

windows

I don't understand why it's so hard to do this on Windows.

I want to spawn a bunch of command prompt windows which will run other scripts. The reason I want this is so I can see all the output from each script neatly (if I have them just be threads/subprocesses in the main window I can't view all the output properly). I also don't want to log the output because it's mostly for viewing progress bars, which don't really work with log files.

So individual parts of my requirements work, but not together:

os.system("start cmd /c {command here}")     # Launches in new command prompt, closes when done 

However, os system won't let me wait until the command finishes (since start is the actual command, the second it opens the new command prompt it's "done")

Similarly if I try:

p = subprocess.Popen(["start", "cmd", "/k", "{command here}"], shell = True) # Needs to be shell since start isn't an executable, its a shell cmd p.wait()    # I can wait until finished (although it too finishes after start finishes) 

So how do I do this? I read somewhere that a solution could be to use processgroup but it's unix only....or something like that

Or if you have a neat way of displaying the output from all the subprocesses in a single window, then I don't need to open a new command prompt and can simply use threads. That works too, but if I have lets say 4 threads downloading something and displaying a progress bar as well as outputting other information I don't know how to display that in a way that can be read (as well as avoiding them all colliding with each other).

PS: This is on Windows Vista. PPS: I'd preferably like a solution that works on Windows, Linux and Mac, I'm focusing on Windows for now but I'd like a solution that works for all three, and I know Windows is the most finicky. I would just substitute "the start cmd /c" for the OS appropriate command.

like image 830
robev Avatar asked Jul 23 '12 15:07

robev


People also ask

How can you start Python through windows command prompt?

Open Command Prompt and type “python” and hit enter. You will see a python version and now you can run your program there.

How do I stop command prompt from closing after execution?

If you want the command prompt cmd widnow to stay open after executing the last command in batch file –you should write cmd /k command at the end of your batch file. This command will prevent the command prompt window from closing and you'll get the prompt back for giving more commands in the cmd window.


2 Answers

Upon reading your comment to my previous answer what you need is:

os.system("start /wait cmd /c {command}") 

Keep the windows command reference always at hand!

like image 80
deStrangis Avatar answered Sep 25 '22 12:09

deStrangis


The accepted answer didn't work for me.
To open on a new command prompt I had to use:

os.system("start /B start cmd.exe @cmd /k mycommand...") 
like image 37
Pedro Lobito Avatar answered Sep 22 '22 12:09

Pedro Lobito