本文翻译自:Python exit commands - why so many and when should each be used?

It seems that python supports many different commands to stop script execution. 似乎python支持许多不同的命令来停止脚本执行。
The choices I've found are: quit() , exit() , sys.exit() , os._exit() 我找到的选择是: quit()exit()sys.exit()os._exit()

Have I missed any? 我错过了吗? What's the difference between them? 它们之间有什么区别? When would you use each? 您什么时候使用?


#1楼

参考:https://stackoom.com/question/1KrBz/Python退出命令-为什么要使用这么多-何时使用


#2楼

Different Means of Exiting 不同的退出方式

os._exit() : os._exit()

  • Exit the process without calling the cleanup handlers. 退出过程而不调用清理处理程序。

exit(0) : exit(0)

  • a clean exit without any errors / problems. 干净的出口,没有任何错误/问题。

exit(1) : exit(1)

  • There was some issue / error / problem and that is why the program is exiting. 存在一些问题/错误/问题,这就是程序退出的原因。

sys.exit() : sys.exit()

  • When the system and python shuts down; 当系统和python关闭时; it means less memory is being used after the program is run. 这意味着程序运行后正在使用的内存更少。

quit() : quit()

  • Closes the python file. 关闭python文件。

Summary 摘要

Basically they all do the same thing, however, it also depends on what you are doing it for. 基本上他们都做相同的事情,但是,这还取决于您要做什么。

I don't think you left anything out and I would recommend getting used to quit() or exit() . 我认为您不会遗漏任何东西,建议您习惯quit()exit()

You would use sys.exit() and os._exit() mainly if you are using big files or are using python to control terminal. 如果使用大文件或使用python控制终端,则主要使用sys.exit()os._exit()

Otherwise mainly use exit() or quit() . 否则,主要使用exit()quit()


#3楼

The functions *quit() , exit() , and sys.exit() function in the same way: they raise the SystemExit exception. 功能*quit() exit()sys.exit()函数以同样的方式:他们提高SystemExit例外。 So there is no real difference, except that sys.exit() is always available but exit() and quit() are only available if the site module is imported. 因此没有真正的区别,只是sys.exit()始终可用,但exit()quit()仅在导入了site模块时才可用。

The os._exit() function is special, it exits immediately without calling any cleanup functions (it doesn't flush buffers, for example). os._exit()函数很特殊,它不调用任何清理函数就立即退出(例如,它不刷新缓冲区)。 This is designed for highly specialized use cases... basically, only in the child after an os.fork() call. 这是专为高度专业化的用例而设计的...基本上,仅在os.fork()调用之后的子os.fork()

Conclusion 结论

  • Use exit() or quit() in the REPL. 在REPL中使用exit()quit()

  • Use sys.exit() in scripts, or raise SystemExit() if you prefer. 在脚本中使用sys.exit() ,或者根据需要raise SystemExit()

  • Use os._exit() for child processes to exit after a call to os.fork() . 使用os._exit()使子进程在调用os.fork()之后退出。

All of these can be called without arguments, or you can specify the exit status, eg, exit(1) or raise SystemExit(1) to exit with status 1. Note that portable programs are limited to exit status codes in the range 0-255, if you raise SystemExit(256) on many systems this will get truncated and your process will actually exit with status 0. 所有这些都可以在不带参数的情况下调用,也可以指定退出状态,例如exit(1)raise SystemExit(1)以退出状态1。请注意,可移植程序仅限于退出状态代码,范围为0- 255,如果在许多系统上raise SystemExit(256) ,它将被截断,并且进程实际上将以状态0退出。

Footnotes 脚注

* Actually, quit() and exit() are callable instance objects, but I think it's okay to call them functions. *实际上, quit()exit()是可调用的实例对象,但是我认为可以调用它们的函数。


#4楼

Let me give some information on them: 让我给他们一些信息:

  1. quit() raises the SystemExit exception behind the scenes. quit()在后台引发SystemExit异常。

    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. 包含此功能是为了帮助不了解Python的人。 After all, one of the most likely things a newbie will try to exit Python is typing in quit . 毕竟,新手尝试退出Python的最有可能的事情之一就是输入quit

    Nevertheless, quit should not be used in production code. 不过, quit 应该在产品代码中使用。 This is because it only works if the site module is loaded. 这是因为它仅在加载了site模块时才有效。 Instead, this function should only be used in the interpreter. 相反,此功能应仅在解释器中使用。

  2. exit() is an alias for quit (or vice-versa). exit()quit的别名(反之亦然)。 They exist together simply to make Python more user-friendly. 它们一起存在只是为了使Python更加用户友好。

    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. 但是,像quit一样, exit在生产代码中被认为是不好用的,应该保留供解释器使用。 This is because it too relies on the site module. 这是因为它也依赖于site模块。

  3. sys.exit() raises the SystemExit exception in the background. sys.exit()在后台引发SystemExit异常。 This means that it is the same as quit and exit in that respect. 这意味着在这方面与quitexit相同。

    Unlike those two however, sys.exit is considered good to use in production code. 但是,与这两者不同, sys.exit被认为可以很好地用于生产代码中。 This is because the sys module will always be there. 这是因为sys模块将始终存在。

  4. os._exit() exits the program without calling cleanup handlers, flushing stdio buffers, etc . os._exit()退出程序, 而无需调用清除处理程序,刷新stdio缓冲区等 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 . 其中最常见的是os.fork创建的子进程。

    Note that, of the four methods given, only this one is unique in what it does. 请注意,在给出的四种方法中,只有一种是唯一的。

Summed up, all four methods exit the program. 总结起来,所有四种方法都退出程序。 However, the first two are considered bad to use in production code and the last is a non-standard, dirty way that is only used in special scenarios. 但是,前两个在生产代码中被认为不好用,最后一个是非标准的,肮脏的方式,仅在特殊情况下使用。 So, if you want to exit a program normally, go with the third method: sys.exit . 因此,如果要正常退出程序,请使用第三个方法: sys.exit


Or, even better in my opinion, you can just do directly what sys.exit does behind the scenes and run: 或者,我认为更好的是,您可以直接在sys.exit执行sys.exit操作并运行:

raise SystemExit

This way, you do not need to import sys first. 这样,您无需首先导入sys

However, this choice is simply one on style and is purely up to you. 但是,此选择只是样式上的一个,完全由您决定。


#5楼

sys.exit is the canonical way to exit. sys.exit是退出的规范方法。

Internally sys.exit just raises SystemExit . 在内部sys.exit只会引发SystemExit However, calling sys.exit is more idiomatic than raising SystemExit directly. 但是,调用sys.exit比直接提高SystemExit更惯用。

os.exit is a low-level system call that exits directly without calling any cleanup handlers. os.exit是一个低级系统调用,它直接退出而不调用任何清除处理程序。

quit and exit exist only to provide an easy way out of the Python prompt. quitexit仅是为了提供一种退出Python提示符的简便方法。 This is for new users or users who accidentally entered the Python prompt, and don't want to know the right syntax. 这适用于新用户或不小心输入Python提示符而又不想知道正确语法的用户。 They are likely to try typing exit or quit . 他们可能会尝试键入exitquit While this will not exit the interpreter, it at least issues a message that tells them a way out: 尽管这不会退出解释器,但它至少会发出一条消息,告诉他们出路:

>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
$

This is essentially just a hack that utilizes the fact that the interpreter prints the __repr__ of any expression that you enter at the prompt. 本质上,这只是一种利用,事实是解释程序会打印您在提示符下输入的任何表达式的__repr__

Python退出命令-为什么要使用这么多?何时使用?相关推荐

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

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

  2. linux下python退出命令_Linux 下 Python按任意键退出方法

    某天在群内有同学问到,在python下我用input或者raw_input都得输入完后回车才能获取到输入的值,那如何实现任意键退出暂停等功能呢,我当时也没有多想,因为接触python时间也不算长,主要 ...

  3. python退出程序-Python退出命令的总结

    Python exit command @(Python入门) [TOC] Which exit() quit() Ctrl+Z Enter sys.exit() raise SystemExit o ...

  4. Ubuntu下如何退出python的命令模式>>>

    Ubuntu下如何退出python的命令模式>>>,可以使用命令exit();或qiut();退出,也可以按住Ctrl+D键退出 可提供远程搭建运行服务 不会调试运行的同学,你只需打 ...

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

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

  6. python语言命令大全-Python常用命令最全合集

    文章目录 一.Python环境配置命令 二.Python 常用命令 三.pip管理工具命令 四.发布包到pypi(官网)命令 Mac 电脑自带python2.x版本,终端输入 python -V // ...

  7. Python常用命令行(持续更新)

    查看 Python 版本的两种方法 1.没有进入到Python shell 在终端输入: python --version 2.已经进入到 Python shell 输入 直接输入 help() 会输 ...

  8. python:命令行与环境

    python:命令行与环境 1.1. 命令行 1.1.1. 接口选项 1.1.2. 通用选项 1.1.3. 其他选项 1.1.4. 不应当使用的选项 1.2. 环境变量 1.2.1. 调试模式变量 C ...

  9. python退出语句_python退出程序语句

    怎样让python运行完了不直接退出 解决办法有两种: 1.代码要对齐,一般Py脚本里面加 input函数是不会自动退出的,正确如下: 2.这种方法是用Input作为控制的,没有加控制语句,建议对输入 ...

最新文章

  1. 周末一起用文本数据库玩玩Code First
  2. Traffic Ccontrol(流量控制)
  3. 9月7日学习内容整理:内置函数
  4. linux c调用wcf服务,Silverlight+WCF实现跨域调用
  5. 22543!Windows 11 新预览版发布
  6. java注册中心nacos_spring-cloud整合nacos做注册中心
  7. sql server排序慢_SQL 查询调优之 where 条件排序字段以及 limit 使用索引的奥秘
  8. 《A First Course in Probability》-chaper3-条件概率和独立性-贝叶斯公式、全概率公式...
  9. java线程——详解Callable、Future和FutureTask
  10. 单片机(ISIS 7 Professional):交通灯代码项目
  11. 基于Java毕业设计在线装机报价系统源码+系统+mysql+lw文档+部署软件
  12. 霜降后养生,做好“三防”
  13. Python基础教程(第三版)读书笔记(8)
  14. icc校色文件使用教程_浅谈如何用ICC文件进行校色,校色前后效果展示
  15. 新项目中用 C# or Java
  16. 区块链溯源是如何实现的?
  17. Hash函数与算法、哈希查找、哈希冲突解决方法总结
  18. 配电房环境智能监测系统及轨道巡检机器人
  19. h5前端调用android拍照功能,H5调用Android拍照和摄像以及选取相册
  20. [7]能不能说一说浏览器缓存

热门文章

  1. 实操高校数据中心vsphere6.0升级6.5,重新规划网络。
  2. 51Nod 1256 乘法逆元 Label:exgcd
  3. DOS口令启用停用的管理员密码
  4. java虚拟机(八)--java性能监控与故障处理工具
  5. [CB] 支付宝区块链的应用- 区块链发票医保理赔.
  6. 关于css中class属性多值问题(亲测,各种情况,附详细代码)
  7. UnrealEngine4和3DMax的配合_准备资源
  8. Java性能优化权威指南-读书笔记(一)-操作系统性能监控工具
  9. 开车十几年了,一些心得和大家分享、探讨
  10. Avalon and Indigo CTP- March 2005提供公开下载!