0

I'm trying to send a End command to the end of this line of code. I've searched everywhere and not able to find a valuable answer..

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)

Wanting to send a Control C of some sort here to stop it without Exiting the program.

2 Answers 2

1

You want to catch a 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)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use control-c.

You just have to catch the KeyboardInterrupt Exception.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.