本文介绍了如何防止使用"ctrl + c"终止正在运行的程序.在Linux中使用python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用python写了一段代码,我在其中提问,用户应该提供输入.有时,用户很难理解这些问题(它们是非英语的).因此,大多数情况下,他们想复制该句子并将其粘贴到Google翻译中.但是,由于此代码在命令提示符下运行,因此他们必须选择文本,然后使用右键单击->复制",才能将文本复制到Google翻译中.有时,有时会误按"ctrl + c"(每个人都可以使用此组合进行复制).这样做将终止代码,并且它们必须重新开始.我需要知道我可以防止这种情况的发生.换句话说,如果他们按下"ctrl + c",则什么也不会发生,并且我的软件不会中止.谢谢

I have written a piece of code in python, in which I am asking questions and users should give their input. Sometimes, these questions are difficult for the user to understand(they are non-english). So most of the time they want to copy paste the sentence into google translate. However, since this code is running in the command prompt,they have to select the text and using "right click --> copy" they can copy the text into google translate. Sometimes, by mistake the press "ctrl+c"(it is natural for everyone to use this combination for copying). Doing this will terminate the code, and they have to start over. I need to know I can prevent this from happening. In other words, if they press "ctrl+c" nothing happens and my software doesn't abort.thanks

推荐答案

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 completely ignored:

import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)

这篇关于如何防止使用"ctrl + c"终止正在运行的程序.在Linux中使用python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 16:45