Python发送命令

时间:2016-09-29 04:31:11

标签: python

我正在尝试将End命令发送到此行代码的末尾。 我到处搜索,无法找到有价值的答案..

if args.time:
    def stopwatch(seconds):
        start = time.time()
        time.clock()    
        elapsed = 0
        while elapsed < seconds:
            elapsed = time.time() - start
            print "Sending for(900) Seconds: %f, seconds count: %02d" % (time.clock() , elapsed) 
            time.sleep(1)  

stopwatch(5)

想要在这里发送某种Control C来阻止它 退出该计划。

2 个答案:

答案 0 :(得分:1)

您想要抓住KeyboardInterrupt

if args.time:
    def stopwatch(seconds):
        start = time.time()
        time.clock()    
        elapsed = 0
        while elapsed < seconds:
            try:
                elapsed = time.time() - start
                print "Sending for(900) Seconds: %f, seconds count: %02d" % (time.clock() , elapsed) 
                time.sleep(1)  
            except KeyboardInterrupt: break

    stopwatch(5)

答案 1 :(得分:0)

您可以使用control-c。

您只需捕获 KeyboardInterrupt 例外。

try:
    # your loop code here
except KeyboardInterrupt:
    # do whatever you want when user presses ctrl+c