Python exit command

@(Python入门)

[TOC]

Which

exit()

quit()

Ctrl+Z Enter

sys.exit()

raise SystemExit

os.exit

How and When

quit

quit raises the SystemExit exception behind the scenes.

Furthermore, if you print it, it will give a message:

>>> print (quit)

Use quit() or Ctrl-Z plus Return to exit

>>>

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

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:

>>> print (exit)

Use exit() or Ctrl-Z plus Return to exit

>>>

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

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

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

Ctrl+Z is a qucik-operation of exit and quit, which means Ctrl+Z is the same with them.

Suggestion

use

raise SystemExit

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.

quit([code=None])

exit([code=None])

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.

Actually, I don't understand the last sentence. Because when I use quit and exit in Python(win10,x64,3.6),it only shows 'Use quit() or Ctrl-Z plus Return to exit'

Figure.1

So, I wonder if there is a problem or bug in there or just my English is awful.

By the way, I have tried to use Ctrl-D to drop out of Python, but failed. Then I found an interesting vent about Ctrl-D.

sys.exit([arg])

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.

what is the meaning of 'raising exception'?

https://docs.python.org/3.4/tutorial/errors.html#exceptions

This may mean that quit, exit, and sys.exit are one kind of SystemExit, but the three would be used in different circumstance.

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 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.

Reference

python退出程序-Python退出命令的总结相关推荐

  1. python退出程序-python退出程序

    广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! python退出程序的方式有两种:os._exit(),sys.exit() 1) ...

  2. python退出程序-Python程序退出方式小结

    对于如何结束一个Python程序或者用Python操作去结束一个进程等,Python本身给出了好几种方法,而这些方式也存在着一些区别,对相关的几种方法看了并实践了下,同时也记录下. 参考: Pytho ...

  3. python中quit函数用法_关于 Python 中的退出命令:sys.exit(n), os._exit(n), quit(), exit()...

    sys.exit(n) 标准的退出函数,会抛出一个 SystemExit 异常,可以在捕获异常处执行其他工作,比如清理资源占用 如果 n 为 0,则表示成功; 非 0 则会产生非正常终止 另外,除了可 ...

  4. python退出程序-python怎么终止程序

    对于如何结束一个Python程序或者用Python操作去结束一个进程等,Python本身给出了好几种方法,而这些方式也存在着一些区别,对相关的几种方法看了并实践了下,同时也记录下. 1. sys.ex ...

  5. Python退出命令-为什么要使用这么多?何时使用?

    本文翻译自:Python exit commands - why so many and when should each be used? It seems that python supports ...

  6. python退出语句_python退出命令

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 要退出python命令行,我必须输入exit(). 如果我输入退出,它提示:us ...

  7. python退出命令-python退出指令

    广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! cmd中如何退出python (1)在命令行上输入exit() (2)在命令行上输 ...

  8. chatgpt赋能python:Python中quit()命令的使用方法和注意事项

    Python中quit()命令的使用方法和注意事项 在Python中,quit()函数是一个非常重要的命令,它被用于退出命令行或交互式Python环境.当你想要结束Python程序或退出Python交 ...

  9. python结束不退出_Python 基本功: 1. Hello world

    上一篇文章帮助你设置了 Python 环境 多多教Python:Python 基本功: 0. 选择环境​zhuanlan.zhihu.com 当然如果你偏好是 Linux 或者 Windows, 那也 ...

最新文章

  1. Cisco交换机解决网络蠕虫病毒***问题
  2. MySQL数据库初识(基础语句)
  3. linux vscode配置HBuilderX雅蓝主题
  4. Scrapy爬虫-win7下创建运行项目
  5. 前端学习(2887):如何短时间内实现v-for proxy代理
  6. 网关 跨域_好文推荐:微服务网关实战—Spring Cloud Gateway
  7. mybatis3文档
  8. HTML颜色码对照表-英文代码、中文描述、十六进制、rgb值
  9. 4选1数据选择器程序及testbench文件,给出仿真波形,分析
  10. Java项目:调查问卷管理系统(java+SpringBoot+Vue+ElementUI+Maven+mysql)
  11. 信息系统项目管理师核心考点(七)软件架构风格
  12. 【Flutter】Flutter 应用生命周期 ( 前台状态 resumed | 后台状态 paused | 非活动状态 inactive | 组件分离状态 detached )
  13. 汉字转拼音 java_Java汉字转拼音工具类完整代码实例
  14. 英文写作中单词的用法
  15. 2018年淘宝新开店铺没有生意没有流量总结经验
  16. BT5 在线视频教程
  17. 写在世界读书日 - 光读书不能让你成为供应链管理专家
  18. Ubuntu 搜狗输入法显示繁体,一直不显示简体
  19. word2vec Parameter Learning Explained学习笔记
  20. 深度学习中的经典基础网络结构(backbone)总结

热门文章

  1. JavaScript正则表达式详解(一)正则表达式入门
  2. 这段时间没有好好的总结了
  3. python中文乱码例子
  4. Spring事务支持:利用继承简化配置
  5. #一周五# VS2015 CTP6, TFS2015 CTP1更新,老衣的开发工具汇总,2015 MVP 社区巡讲
  6. java step1:基础知识5(java中Timer和TimerTask的使用)
  7. 配置ssh_config
  8. 【原创】再见,CVT----记搓人的第一次找实习
  9. python字符串截取split-python实现字符串完美拆分split()的方法
  10. python列表按照指定顺序排序-Python3实现对列表按元组指定列进行排序的方法分析...