How to prevent a running program from ending with "ctrl + c" on Linux using python?

I wrote a snippet of code in python in which I ask questions, and users should contribute. Sometimes these questions are difficult for the user to understand (they are not English). Therefore, most of the time they want to copy paste the sentence into google translate. However, since this code is run on the command line, they must select the text and use β€œright click β†’ copy”, they can copy the text to google translate. Sometimes, by mistake, press "ctrl + c" (for all users it is natural to use this combination for copying). Doing this will end the code, and they should start all over again. I need to know that I can prevent this. In other words, if they press "ctrl + c", nothing happens and my software does not interrupt. thanks

+4
source share
3 answers
import signal def SigIntHand(SIG, FRM): print("Please Right click-copy. Ctrl-C does not work on the cmd prompt") signal.signal(signal.SIGINT, SigIntHand) 

or if you want it to be completely ignored:

 import signal signal.signal(signal.SIGINT, signal.SIG_IGN) 
+1
source

When you press ctrl + c, it will send SIGINT to the current process. You can catch him as described here .

Read more about the different types of signals here .

+2
source

If you use X, the text is usually copied to the clipboard after selecting it. Just paste it with the middle mouse button or paste Shift +.

0
source

All Articles