干净地退出python脚本

时间:2012-05-25 19:16:10

标签: python exit-code

我正在尝试退出python脚本,而不会出现恼人的错误消息:

Traceback (most recent call last):
  File "<pyshell#27>", line 3, in <module>
    sys.exit()
SystemExit

我尝试了很多东西,但没有一个有效。这是一个例子:

while True:
    print "hi", #this just tests to see if I have exited.
    try:
        sys.exit()
    except SystemExit:
        print "Exited"

注意:解决方案不一定要接近这段代码,这只是我尝试过的一个例子

1 个答案:

答案 0 :(得分:5)

当我在Python控制台(在Windows或Linux下)运行时,我在使用sys.exit(-1)时没有收到任何类型的错误消息。但是,iPython会给我

An exception has occurred, use %tb to see the full traceback.

SystemExit: -1

我怀疑你看到错误消息,因为你在某种IDE中工作,是这样吗?

相关问题