【问题标题】:Exiting a python script cleanly干净地退出python脚本
【发布时间】:2012-06-01 09:01:30
【问题描述】:

我正在尝试退出 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"

注意:解决方案不必与此代码接近,这只是我尝试过的一个示例

【问题讨论】:

  • 你在什么系统上运行?
  • 您是否通过 IDE(例如 IDLE)运行脚本?
  • @Joel,是的空闲。这应该有所作为吗?
  • @Shurane Windows 7,python 2.7.2。

标签: python exit-code


【解决方案1】:

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

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

SystemExit: -1

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

【讨论】:

  • 是的,它是 IDE。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
相关资源
最近更新 更多