Python退出命令的总结

Python017

Python退出命令的总结,第1张

@(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 production code. This is because it only works if the site module 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 production code and should be reserved for use in the interpreter. This is because it too relies on the site module.

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 production code. This is because the sys module 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 module (which is imported automatically during 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 produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdevelopedUnix 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

我们这里使用的是python3.6版本,我们先确认python版本及python正常工作。如下面图中所示,python版本是3.6.5,可以正常登陆。

请点击输入图片描述

我们首先来看第一种方式,使用python提供的exit()函数,如下面两张图中所示,分别是linux平台和windows平台上的exit()函数执行结果。可以看到,都可以正常退出。唯一的区别是,windows平台结束后多输出了一行空行。

请点击输入图片描述

请点击输入图片描述

再看python提供的第二个函数quit(),同样是linux和windows平台,执行结果一致。都可以正常退出,windows平台多输出一行空行。

请点击输入图片描述

请点击输入图片描述

函数方式看完了以后,我们来看快捷键退出的方式。先看快捷键Ctrl +D的组合。如下面两张图中所示,分别是linux平台和windows平台上使用Ctrl+D的组合键,可以看到,linux平台上直接退出,而windows平台上先出现一个^D的字符,敲回车后,提示语法错误,无法正常退出。这里就有区别了。

请点击输入图片描述

请点击输入图片描述

再看第二张快捷键的组合方式Ctrl+Z,如下面两张图中所示,分别是linux平台和windows平台,linux平台上显示进程已结束,程序退出。而在windows平台上按下Ctrl+Z组合键时,只会显示^Z字符,然后再敲一下回车才能正常退出,这个命令上,linux平台和windows平台也有区别。

请点击输入图片描述

请点击输入图片描述

最后我们再来看一个linux平台上的python命令行退出方式,这个方式只适合一些特殊场合,例如通过xshell工具连接到linux服务器上运行python命令行时,xshell卡死了。这个时候就可以用这个方法。打开另外一个命令行,输入命令:

ps aux  | grep python

找到所有python命令行,比如我这里就有两个,进程ID分别是12525 12655,如下面图中所示:

请点击输入图片描述

然后使用kill命令杀掉这两个进程,如下面图中所示,杀掉进程后,在启动python命令行的shell界面,就会显示进程被退出。这种属于不正常的退出,只适合特殊情况。到这里,几种退出方式就讲解完毕啦。

请点击输入图片描述

请点击输入图片描述