pythonquit
⑴ 如何在python程序退出的時候,強制運行一些命令呢
程序一定會運行到clean()函數,但是,如果你代碼寫的多,你就應該知道,濫用try...except...會讓你非常痛苦。例如它突然給你列印一個運行異常: 1。你根本不知道是哪裡出了問題,也不知道具體出了什麼問題。為了找到問題,你必須讓程序把錯誤爆出來。但這樣一來,clean()又不能正常運行了。Python退出時強制運行一段代碼
有什麼辦法,既能讓程序報錯,又能在報錯已經還能運行clean()呢?
這個時候,我們就可以使用Python自帶的atexit這個模塊了。它的使用方法非常簡單:
import atexit
@atexit.register
def clean():
print('清理環境相關的代碼')
setup()
test()
這樣一來,我們不需要顯式調用clean函數了。無論程序正常結束,還是程序異常報錯,clean函數裡面的內容總會執行。
⑵ Python退出命令的總結
@(Python入門)
[TOC]
quit raises the SystemExit exception behind the scenes.
Furthermore, if you print it, it will give a message:
This functionality was included to help people who do not know Python. After all, one of the most likely things a newbie will try to exit Python is typing in quit .
Nevertheless, quit should not be used in proction code. This is because it only works if the site mole is loaded. Instead, this function should only be used in the interpreter.
exit is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly.
Furthermore, it too gives a message when printed:
However, like quit , exit is considered bad to use in proction code and should be reserved for use in the interpreter. This is because it too relies on the site mole.
sys.exit raises the SystemExit exception in the background. This means that it is the same as quit and exit in that respect.
Unlike those two however, sys.exit is considered good to use in proction code. This is because the sys mole will always be there.
os._exit exits the program without calling cleanup handlers, flushing stdio buffers, etc . Thus, it is not a standard way to exit and should only be used in special cases. The most common of these is in the child process(es) created by os.fork .
Ctrl+Z is a qucik-operation of exit and quit , which means Ctrl+Z is the same with them.
use
to exit, so u don't need to import sys first.
The site mole (which is imported automatically ring startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs.
Objects that when printed, print a message like 「Use quit() or Ctrl-D (i.e. EOF, end of file) to exit」, and when called, raise SystemExit with the specified exit code.
Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.
The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered 「successful termination」 and any nonzero value is considered 「abnormal termination(結局)」 by shells and the like. Most systems require it to be in the range 0–127, and proce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit ( "some error message" ) is a quick way to exit a program when an error occurs.
Since exit() ultimately 「only」 raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
Changed in version 3.6: If an error occurs in the cleanup after the Python interpreter has caught SystemExit (such as an error flushing buffered data in the standard streams), the exit status is changed to 120.
https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used
https://docs.python.org/3/library/constants.html#quit
https://docs.python.org/3/library/sys.html#sys.exit
http://grokbase.com/t/python/python-list/042qh9j55e/gripe-use-ctrl-d-i-e-eof-to-exit
⑶ python如何在終端退出
有時為了方便經常會在命令行敲python代碼,但是用了不記得怎麼退出了,只能關閉窗口,
退出方法:在>>>後面 輸入 exit()就行了
cmd中如何退出Python
(1)在命令行上輸入exit()
(2)在命令行上輸入quit()
推薦學習《python教程》
好像還有一種方法是在命令行上輸入Ctrl+Z,
除此之外,還可以使用
quit() 回車
control+z 回車
注意上面exit和quit後面都有括弧()。
⑷ 退出python環境應該輸入什麼
我們這里使用的是python3.6版本,我們先確認python版本及python正常工作。如下面圖中所示,python版本是3.6.5,可以正常登陸。
⑸ python里怎麼終止程序的執行
quit() exit()
執行到此命令時,程序終止。
如果是程序陷入死循環,想強制結束,則按Ctrl + C。這個特別關鍵。
Python的設計哲學是「優雅」、「明確」、「簡單」。因此,Perl語言中「總是有多種方法來做同一件事」的理念在Python開發者中通常是難以忍受的。Python開發者的哲學是「用一種方法,最好是只有一種方法來做一件事」。
在設計Python語言時,如果面臨多種選擇,Python開發者一般會拒絕花俏的語法,而選擇明確的沒有或者很少有歧義的語法。由於這種設計觀念的差異,Python源代碼通常被認為比Perl具備更好的可讀性,並且能夠支撐大規模的軟體開發。這些准則被稱為Python格言。在Python解釋器內運行import this可以獲得完整的列表。
擴展質料:
Python在執行時,首先會將.py文件中的源代碼編譯成Python的byte code(位元組碼),然後再由Python Virtual Machine(Python虛擬機)來執行這些編譯好的byte code。這種機制的基本思想跟Java,.NET是一致的。
然而,Python Virtual Machine與Java或.NET的Virtual Machine不同的是,Python的Virtual Machine是一種更高級的Virtual Machine。這里的高級並不是通常意義上的高級,不是說Python的Virtual Machine比Java或.NET的功能更強大。
而是說和Java 或.NET相比,Python的Virtual Machine距離真實機器的距離更遠。或者可以這么說,Python的Virtual Machine是一種抽象層次更高的Virtual Machine。
基於C的Python編譯出的位元組碼文件,通常是.pyc格式。
除此之外,Python還可以以交互模式運行,比如主流操作系統Unix/Linux、Mac、Windows都可以直接在命令模式下直接運行Python交互環境。直接下達操作指令即可實現交互操作。
參考資料:Python-網路
⑹ python編譯器怎麼退出閱讀模式
關閉:quit()或exit()或者快捷鍵Ctrl+d。
python退出程序的方式有兩種:os._exit(),sys.exit()。
隨著Python生態系統的不斷發展壯大,業界出現了許多代碼庫、框架、以及編譯器,可用來加速Python,克服其應用限制,甚至能夠將Python與其他編程語言(如:Java、C、C++、JavaScript等)協同使用。