如何使用python中的键盘输入继续for循环?

如何使用python中的键盘输入继续for循环?,python,linux,io,Python,Linux,Io,我有一个for循环,它使用subprocess.call()调用linux程序。有时它花费的时间太长,我希望能够跳到continue循环。当subprocess.call()正在运行时,有没有办法监视键盘输入(比如s for x in y: if lookforkeyboardinput = s: continue subprocess.call(['program', x]) 类似于此。使用键盘中断异常: for x in y: try:

我有一个for循环,它使用
subprocess.call()
调用linux程序。有时它花费的时间太长,我希望能够跳到
continue
循环。当
subprocess.call()
正在运行时,有没有办法监视键盘输入(比如
s

for x in y:
    if lookforkeyboardinput = s:
        continue
    subprocess.call(['program', x])

类似于此。

使用键盘中断异常:

for x in y:
    try:
        subprocess.call(['program', x])
    except (KeyboardInterrupt, SystemExit):
        continue
例如:

from time import sleep

for x in range(0, 5):
    try:
        sleep(5)
        print x
    except (KeyboardInterrupt, SystemExit):
        continue
输出:

hedde-mpb:Desktop hedde$ python test.py 
1 
^C  // pressed right after 1 appears, slightly over 5 seconds later 3 appears
3

这将导致程序无法通过ctrl+c运行。如何将键设置为
s