Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

ctrl c exception python

#Try the following:
try:
    # DO THINGS
except KeyboardInterrupt:
    # quit
    sys.exit()
Comment

how to catch ctrl c in python

import signal
import time
import readchar
 
def handler(signum, frame):
    msg = "Ctrl-c was pressed. Do you really want to exit? y/n "
    print(msg, end="", flush=True)
    res = readchar.readchar()
    if res == 'y':
        print("")
        exit(1)
    else:
        print("", end="
", flush=True)
        print(" " * len(msg), end="", flush=True) # clear the printed line
        print("    ", end="
", flush=True)
 
 
signal.signal(signal.SIGINT, handler)
 
count = 0
while True:
    print(f"{count}", end="
", flush=True)
    count += 1
    time.sleep(0.1)
Comment

how to catch ctrl c in python

import signal
import time
 
def handler(signum, frame):
    res = input("Ctrl-c was pressed. Do you really want to exit? y/n ")
    if res == 'y':
        exit(1)
 
signal.signal(signal.SIGINT, handler)
 
count = 0
while True:
    print(count)
    count += 1
    time.sleep(0.1)
Comment

PREVIOUS NEXT
Code Example
Python :: get all file in folder python 
Python :: npr python 
Python :: install python 3.6 on centos 
Python :: passing user instance django form submission 
Python :: add two datetime python 
Python :: set http response content type django 
Python :: python merge two dictionaries in a single expression 
Python :: python find in largest 3 numbers in an array 
Python :: accept user input in python 
Python :: The Python path in your debug configuration is invalid. 
Python :: armstrong python 
Python :: get mac address python 
Python :: qlistwidget item clicked event pyqt 
Python :: how to clear the screen of the terminal using python os 
Python :: twin axis python 
Python :: python exit for loop 
Python :: how to make a countdown in pygame 
Python :: merge three dataframes pandas based on column 
Python :: Get Current Date using today method 
Python :: rnadom number python 
Python :: assign multiple variables in python 
Python :: dynamic array python numpy 
Python :: custom save django 
Python :: pandas select 2nd row 
Python :: python font 
Python :: palindrome string python 
Python :: xor string python 
Python :: python create env ubuntu 
Python :: reading json file in python 
Python :: how to extract integers from string python 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =