I am running multiple processes (hundreds), each of which is in python and invoked using:
command = 'start cmd /k call python %s' % (some_py_prog)
os.system(command)
where the /k leaves the cmd window open after execution. This is good to inspect for errors. However, as I call hundreds of jobs, my screen gets cluttered.
How then to make python close its own host cmd window on successful completion only? I need the errored jobs to remain visible.
At the command prompt, type exit. Depending on the device configuration, you may be presented with another menu, for example: Access selection menu: a: Admin CLI s: Shell q: Quit Select access or quit [admin] : Type q or quit to exit.
Python exit commands: quit(), exit(), sys. exit() and os. _exit()
Ensure that you are running the correct version of Python (at least Python 3). If you are on a Mac you can also check the version you are running by typing python3 -version in Terminal. You will then see the shell prompt >>> indicating that you are in the Python shell. To exit the python shell, simply enter exit().
Short answer: just use /c
instead of /k
will cause the cmd script to exit as soon as the command terminates, which is what you ask for:
command = 'start cmd /c call python %s' % (some_py_prog)
os.system(command)
But you are stacking some unnecessary cmd.exe
here: one for the os.system
call, one for the explicit cmd.c
. You could simply use the subprocess
module:
subprocess.call('python %s' % (some_py_prog),
creationflags = subprocess.CREATE_NEW_CONSOLE)
The creation flag is enough to ask the system to start the new process in a new console that will be closed as soon as the command will be terminated. Optionaly, you can add the shell=True
option if you need it to pre-process some cmd goodies like io redirection.
(more details in that other answer from mine, specially in eryksun's comment)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With