sending ctrl C to a process

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Mar 28 21:21:07 EST 2006


Em Ter, 2006-03-28 às 18:14 -0800, s99999999s2003 at yahoo.com escreveu:
> It will need a Ctrl-C in order to break out of the program.
> I wish to run this program using python (either thru os.system() or
> some other subprocess modules) and how can i pass Ctrl-C to this
> program to terminate it in python? 

$ python2.4 && echo " **** Python terminates!"
Python 2.4.2 (#2, Mar 27 2006, 21:07:39)
[GCC 4.0.3 (Debian 4.0.3-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ^C
KeyboardInterrupt
>>> try:
...     while 1:
...             pass
... except KeyboardInterrupt:
...     print "\nHey, why did you press Ctrl-C?!"
... except:
...     print "\nStrange..."
...
^C
Hey, why did you press Ctrl-C?!
>>> import sys
>>> try:
...     while 1:
...             pass
... except KeyboardInterrupt:
...     sys.exit(0)
...
^C
 **** Python terminates!
$

HTH,

-- 
Felipe.




More information about the Python-list mailing list