Blocking ctrl-c to os.popen() or os.system()

Chad Netzer cnetzer at
Sun Jan 5 04:31:29 CET 2003

On Saturday 04 January 2003 14:00, Sandeep Gupta wrote:
> I am executing a command from python via os.popen().  While the
> command is executing, if I type ctrl-c, the command receives the
> interrupt.

What is your system?  On Linux, with a trivial (and perhaps foolish) example 
it seems that the Python interpreter does indeed get the ctrl-c:

import os
import time

if __name__ == '__main__':
    cmd_str = "sleep 20"
    print 'Running "%s"' % cmd_str
    os.popen2( cmd_str )

    print "Python now sleeping for 20 seconds."
    except KeyboardInterrupt:
        print "ctrl-c was pressed"

$ python 
Running "sleep 20"
Python now sleeping for 20 seconds.
ctrl-c was pressed

Note - I pressed ctrl-c to get that result. :)

Bay Area Python Interest Group -

Chad Netzer
cnetzer at

More information about the Python-list mailing list