關於 Python 中的退出命令:sys.exit(n), os._exit(n), quit(), exit()


sys.exit(n)

標准的退出函數,會拋出一個 SystemExit 異常,可以在捕獲異常處執行其他工作,比如清理資源占用
如果 n 為 0,則表示成功; 非 0 則會產生非正常終止
另外,除了可以傳遞整型,也可以傳遞對象,比如 None 這將等價於數字 0,如果值不是 None 那么其他類型的對象都視為數字 1(也就是非正常終止)
在實際應用中,可以使用 sys.exit("YOUR ERROR MSG") 當做快捷退出程序的方式並附加一段消息

參考

# Python program to demonstrate 
# sys.exit()   
import sys 
age = 17
if age < 18: 
    # exits the program 
    sys.exit("Age less than 18")     
else: 
    print("Age is not less than 18")

輸出

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

SystemExit: Age less than 18

官方文檔

os._exit(n)

直接退出進程,並返回狀態 n,不處理清理過程,不刷新標准輸入輸出 buffers。
標准退出請使用 sys.exit(n), os._exit(n) 一般用於 os.fork() 創建的子進程。

# Python program to explain os._exit() method   
    
# importing os module    
import os  
    
# Create a child process  
# using os.fork() method   
pid = os.fork()  
    
    
# pid greater than 0  
# indicates the parent process   
if pid > 0:  
    
    print("\nIn parent process")  
    # Wait for the completion   
    # of child process and      
    # get its pid and   
    # exit status indication using  
    # os.wait() method  
    info = os.waitpid(pid, 0)  
    
        
    # os.waitpid() method returns a tuple  
    # first attribute represents child's pid  
    # while second one represents  
    # exit status indication  
    
    # Get the Exit code   
    # used by the child process  
    # in os._exit() method  
        
    # firstly check if  
    # os.WIFEXITED() is True or not  
    if os.WIFEXITED(info[1]) :  
        code = os.WEXITSTATUS(info[1])  
        print("Child's exit code:", code)  
    
else :  
    print("In child process")  
    print("Process ID:", os.getpid())  
    print("Hello ! Geeks")  
    print("Child exiting..")  
        
    # Exit with status os.EX_OK  
    # using os._exit() method  
    # The value of os.EX_OK is 0          
    os._exit(os.EX_OK) 

輸出:

In child process
Process ID: 25491
Hello ! Geeks
Child exiting..

In parent process
Child's exit code: 0

官方文檔

quit()

只被用於解釋器

# Python program to demonstrate 
# quit() 
  
for i in range(10): 
      
    # If the value of i becomes  
    # 5 then the program is forced 
    # to quit 
    if i == 5: 
          
        # prints the quit message 
        print(quit) 
        quit() 
    print(i) 

輸出:

0
1
2
3
4
Use quit() or Ctrl-D (i.e. EOF) to exit

exit()

quit()exit() 的出現更多是出於用戶友好的目的(因為 exit() 更常見一些,更符合用戶習慣一些?)

# Python program to demonstrate 
# exit() 
for i in range(10): 
      
    # If the value of i becomes  
    # 5 then the program is forced 
    # to exit 
    if i == 5: 
          
        # prints the exit message 
        print(exit) 
        exit() 
    print(i) 

輸出:

0
1
2
3
4
Use exit() or Ctrl-D (i.e. EOF) to exit

總結

正常使用 sys.exit(n)
os.fork() 創建的子進程使用 os._exit(n)
在解釋器調試使用 quit()/exit()


Python exit commands: quit(), exit(), sys.exit() and os._exit()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM