我想知道如何退出Python而不在输出上有回溯转储.
我仍然希望能够返回错误代码,但我不想显示回溯日志.
我希望能够在exit(number)没有跟踪的情况下退出,但是在异常(不是退出)的情况下,我想要跟踪.
我更喜欢使用Notepad ++进行开发,
如何通过Notepad ++在Python中执行文件?
我正在用Python编写命令行实用程序,因为它是生产代码,所以应该能够干净地关闭而不会将大量的东西(错误代码,堆栈跟踪等)转储到屏幕上.这意味着我需要捕获键盘中断.
我尝试过使用try catch块,如:
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print 'Interrupted'
sys.exit(0)
Run Code Online (Sandbox Code Playgroud)
并捕捉信号本身(如在这篇文章中):
import signal
import sys
def sigint_handler(signal, frame):
print 'Interrupted'
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
Run Code Online (Sandbox Code Playgroud)
这两种方法在正常操作期间似乎都能很好地工作.但是,如果在应用程序结束时清理代码期间出现中断,Python似乎总是在屏幕上打印一些东西.捕获中断给出了
^CInterrupted
Exception KeyboardInterrupt in <bound method MyClass.__del__ of <path.to.MyClass object at 0x802852b90>> ignored
Run Code Online (Sandbox Code Playgroud)
而处理信号也给出了
^CInterrupted
Exception SystemExit: 0 in <Finalize object, dead> ignored
Run Code Online (Sandbox Code Playgroud)
要么
^CInterrupted
Exception SystemExit: 0 in <bound method MyClass.__del__ of <path.to.MyClass object at 0x802854a90>> ignored
Run Code Online (Sandbox Code Playgroud)
这些错误不仅难看,而且对于没有源代码的最终用户来说也不是很有帮助!
此应用程序的清理代码相当大,因此真正的用户可能会遇到此问题.有没有办法捕获或阻止此输出,或者它只是我必须处理的东西?