簡體   English   中英

干凈地退出python腳本

[英]Exiting a python script cleanly

我試圖退出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"

注意:解決方案不一定要接近這個代碼,這只是我嘗試過的一個例子

當我在Python控制台(在Windows或Linux下)運行它時,當我使用sys.exit(-1)時,我沒有收到任何類型的錯誤消息。 但是,iPython會給我

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

SystemExit: -1

我懷疑你看到錯誤消息,因為你在某種IDE工作,是這樣嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM