Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard interrupts and os.system vs subprocess.call

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?

like image 208
facha Avatar asked Sep 11 '13 11:09

facha


1 Answers

You can catch the keyboard interrupt in Python with an exception handler:

try:
    retcode = subprocess.call(args)
except KeyboardInterrupt:
    pass # ignore CTRL-C
like image 149
Martijn Pieters Avatar answered Oct 22 '22 16:10

Martijn Pieters