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 :: copy to clipboard python 
Python :: ursina editor camera 
Python :: keras plot history 
Python :: matplotlib figsize 
Python :: bold text variable in python 
Python :: pandas get rows with missing data 
Python :: python time code 
Python :: how to print hostname in python 
Python :: pyspark convert float results to integer replace 
Python :: use incognito in selenium 
Python :: loop in reverse order using django template 
Python :: pandas rename specific column 
Python :: ubuntu remove python 2.7 
Python :: sns title 
Python :: how to delete row pandas in for loop 
Python :: find text between two strings regex python 
Python :: ind vs wi 
Python :: convert column to numeric pandas 
Python :: python euclidean algorithm 
Python :: linux python installation wheel 
Python :: python regex replace all non alphanumeric characters 
Python :: python sort a list of tuples 
Python :: make string numeric pandas 
Python :: create boto3 s3 client with credentials 
Python :: use selenium without opening browser 
Python :: execute command and get output python 
Python :: pygame get mouse position 
Python :: get date and time in python 
Python :: adding whitenoise to middleware in django 
Python :: python program to shutdown computer when user is not present 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =