I'm writing a small CLI in python (with help of cmd module). Currently I'm trying to replace all os.system
occurrences with subprocess.call
.
The problem I'm facing is that if I run an external script with os.system
, after I hit CTRL-C only a subshell terminates (I get back into my CLI). When I run the same script with subprocess.call
and hit CTRL-C, the script and my CLI both terminate the execution.
Is there a way to mimic the os.system
behaviour with subprocess.call
?
You can catch the keyboard interrupt in Python with an exception handler:
try:
retcode = subprocess.call(args)
except KeyboardInterrupt:
pass # ignore CTRL-C
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