当前位置:首页 » 编程语言 » pythonquit

pythonquit

发布时间: 2023-04-20 02:46:56

⑴ 如何在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等)协同使用。

热点内容
怎么给自己手机写一个脚本 发布:2024-11-01 20:23:41 浏览:241
c语言大小写判断 发布:2024-11-01 20:21:53 浏览:130
php的点餐系统源码 发布:2024-11-01 20:13:53 浏览:714
拜占庭算法 发布:2024-11-01 20:10:31 浏览:357
xcode编译参数 发布:2024-11-01 20:00:04 浏览:665
苹果5怎么设置密码锁屏 发布:2024-11-01 19:54:55 浏览:124
宝塔上传文件夹 发布:2024-11-01 19:39:50 浏览:257
java云编译器 发布:2024-11-01 19:34:24 浏览:385
免费源码分享网 发布:2024-11-01 19:29:19 浏览:855
硬盘8mb缓存 发布:2024-11-01 19:20:02 浏览:192