如何防止使用“ctrl”c“终止正在运行的程序在Linux中使用python?
我用 python 编写了一段代码,我在其中提出问题,用户应该给出他们的输入。有时,这些问题对于用户来说很难理解(它们不是英语)。所以大多数时候他们想将句子复制粘贴到谷歌翻译中。但是,由于此代码在命令提示符中运行,因此他们必须选择文本并使用“右键单击 --> 复制”将文本复制到谷歌翻译中。有时,会误按“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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者如果你想完全忽略它:
or if you want it completely ignored:
当您按下 ctrl+c 时,它会向正在运行的进程发送 SIGINT。您可以按照此处所述捕获它。
您可以在此处找到有关不同类型信号的更多信息。
When you hit ctrl+c it sends SIGINT to the running process. You can catch it as described here.
You can find more about the different types of signals here.
如果使用 X,则文本在选择后通常会复制到剪贴板。只需使用鼠标中键或 Shift+insert 粘贴即可。
If using X, the text is normally copied to the clipboard once it's selected. Just paste it using middle mouse button or Shift+insert.