Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的脚本有问题。我想从头到尾为整个脚本做一个事件监听器。如果有人按下 ctrl-c 或它应该忽略的东西,或者以打印结束 scipt。
如果需要,我可以向您展示我的代码。
我尝试了信号和系统,但我不知道如何正确使用它
用 print 结束脚本的一种简单方法是将整个脚本包装在 try/except 块中:
import sys try: while 1: print 'To infinity and beyond!' # etc etc... except KeyboardInterrupt: print 'Handling the keyboard interrupt...'
中断仍然会杀死脚本,但我认为这是任何用户都希望的行为;我个人不建议尝试覆盖它。