标签归档:exit-code

Python中exit(0)和exit(1)之间的区别

问题:Python中exit(0)和exit(1)之间的区别

exit(0)exit(1)Python 和有什么不一样?

我尝试环顾四周,但没有在这些线上找到具体问题。如果已经回答,则链接就足够了。

What’s the difference between exit(0) and exit(1) in Python?

I tried looking around but didn’t find a specific question on these lines. If it’s already been answered, a link would be sufficient.


回答 0

0和1是退出代码。

exit(0) 意味着干净出口,没有任何错误/问题

exit(1) 表示存在一些问题/错误/问题,这就是程序退出的原因。

这不是特定于Python的,非常普遍。非零退出代码被视为异常退出,有时,错误代码指示问题所在。错误代码为零表示成功退出。

这对于其他程序,shell,调用方等很有用,以了解您的程序发生了什么并相应地进行。

0 and 1 are the exit codes.

exit(0) means a clean exit without any errors / problems

exit(1) means there was some issue / error / problem and that is why the program is exiting.

This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.

This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.


回答 1

它确定程序结束运行时的退出状态(通常为0表示成功,而1表示错误)。

它不是Python独有的,确切的效果取决于您的操作系统以及该程序的调用方式(尽管在99%的时间中,如果您仅运行Python脚本,那都没有关系)。

This determines the exit status of the program when it finishes running (generally, 0 for success and 1 for error).

It is not unique to Python, and the exact effect depends on your operating system and how the program is called (though 99% of the time, if you’re just running Python scripts, it doesn’t matter).


回答 2

所有C程序(包括Python)的标准约定用于exit(0)表示成功,exit(1)或其他任何非零值(在1..255范围内)表示失败。超出0..255范围的任何值都将以256为模(退出状态存储在8位值中)。有时,它将被视为已签名(因此,您可能会看到-128,-127等),但更常见的是将其视为未签名。

此状态可用于调用Python的代码。尽管不同平台上的非零退出状态的含义可能有所不同,但该约定适用于所有平台。

The standard convention for all C programs, including Python, is for exit(0) to indicate success, and exit(1) or any other non-zero value (in the range 1..255) to indicate failure. Any value outside the range 0..255 is treated modulo 256 (the exit status is stored in an 8-bit value). Sometimes, that will be treated as signed (so you might see -128, -127, etc) but more usually it is treated as unsigned.

This status is available to the code that invoked Python. This convention applies across platforms, though the meaning of non-zero exit status can vary on different platforms.


回答 3

您传递给该exit()函数的数字只是您程序的返回码,该码已提供给操作系统。从程序的角度来看,没有什么区别:在两种情况下执行都将结束,并且提供给函数的值将提供给OS。但是某些工具和脚本会考虑程序的退出代码。大多数工具成功时返回0,非零则表示错误。

因此,如果要从脚本,自动化工具或考虑返回代码的其他某些软件(例如IDE)运行程序,则必须注意返回的内容。

如有疑问,只需返回0即可表示一切正常。

The number you pass to the exit() function is simply your program’s return code, which is given to the operating system. From your program’s point of view, there is no difference: execution will end in both cases, and the value supplied to the function will be given to the OS. But some tools and scripts take into account the program’s exit code. Most tools return 0 when they succeed and nonzero to indicate an error.

So, if your program will be run from a script, an automated tool or from some other software that takes into account the return code (such as an IDE), you must be careful on what you return.

When in doubt, just return 0 to indicate everything is OK.


回答 4

exit(0):这将导致程序退出并成功终止。

exit(1):这将导致程序以系统特定的含义退出。

在许多系统上,exit(1)发出某种故障信号,但是不能保证。

我记得,C标准仅识别三个标准出口值:

  • EXIT_SUCCESS -成功终止
  • EXIT_FAILURE -终止失败
  • 0 – 和…一样 EXIT_SUCCESS

exit(0): This causes the program to exit with a successful termination.

exit(1): This causes the program to exit with a system-specific meaning.

On many systems, exit(1) signals some sort of failure, however there is no guarantee.

As I recall, the C standard only recognizes three standard exit values:

  • EXIT_SUCCESS — successful termination
  • EXIT_FAILURE — unsuccessful termination
  • 0 — same as EXIT_SUCCESS

Python中的退出代码

问题:Python中的退出代码

我收到一条消息说script xyz.py returned exit code 0。这是什么意思?

Python中的退出代码是什么意思?那里有多少?哪些重要?

I got a message saying script xyz.py returned exit code 0. What does this mean?

What do the exit codes in Python mean? How many are there? Which ones are important?


回答 0

您在脚本中寻找的是对的调用sys.exit()。该方法的参数作为退出代码返回到环境。

该脚本很可能从未调用过exit方法,并且0是默认的退出代码。

What you’re looking for in the script is calls to sys.exit(). The argument to that method is returned to the environment as the exit code.

It’s fairly likely that the script is never calling the exit method, and that 0 is the default exit code.


回答 1

文档中sys.exit

可选参数arg可以是给出退出状态的整数(默认为零),也可以是其他类型的对象。如果它是整数,则shell等将零视为“成功终止”,而将任何非零值视为“异常终止”。大多数系统要求它的范围是0-127,否则会产生不确定的结果。某些系统具有为特定的退出代码分配特定含义的约定,但是这些通常不完善。Unix程序通常将2用于命令行语法错误,将1用于所有其他类型的错误。

在外壳程序脚本中使用退出代码的一个示例。在bash中,您可以检查特殊变量$?的最后退出状态:

me@mini:~$ python -c ""; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(0)"; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(43)"; echo $?
43

我个人尝试使用在/usr/include/asm-generic/errno.hLinux系统上找到的退出代码,但是我不知道这样做是否正确。

From the documentation for sys.exit:

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.

One example where exit codes are used are in shell scripts. In bash you can check the special variable $? for the last exit status:

me@mini:~$ python -c ""; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(0)"; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(43)"; echo $?
43

Personally I try to use the exit codes I find in /usr/include/asm-generic/errno.h (on a Linux system), but I don’t know if this is the right thing to do.


回答 2

作为记录,您可以使用此处定义的POSIX标准退出代码。

例:

import sys, os

try:
    config()
except:
    sys.exit(os.EX_CONFIG) 
try:
    do_stuff()
except:
    sys.exit(os.EX_SOFTWARE)
sys.exit(os.EX_OK) # code 0, all ok

For the record, you can use POSIX standard exit codes defined here.

Example:

import sys, os

try:
    config()
except:
    sys.exit(os.EX_CONFIG) 
try:
    do_stuff()
except:
    sys.exit(os.EX_SOFTWARE)
sys.exit(os.EX_OK) # code 0, all ok

回答 3

有一个errno模块定义标准退出代码:

例如,权限被拒绝是错误代码13

import errno, sys

if can_access_resource():
    do_something()
else:
    sys.exit(errno.EACCES)

There is an errno module that defines standard exit codes:

For example, Permission denied is error code 13:

import errno, sys

if can_access_resource():
    do_something()
else:
    sys.exit(errno.EACCES)

回答 4

退出代码0通常表示“这里没有问题”。但是,如果脚本的程序员未遵循约定,则可能必须查阅源代码以了解其含义。通常,将非零值作为错误代码返回。

Exit codes of 0 usually mean, “nothing wrong here.” However if the programmer of the script didn’t follow convention you may have to consult the source to see what it means. Usually a non-zero value is returned as an error code.


回答 5

答案是“取决于零退出码的含义”

但是,在大多数情况下,这意味着“一切都很好”


我喜欢POSIX:

因此,在外壳上,我将输入:

python script.py && echo 'OK' || echo 'Not OK'

如果我的python脚本调用,sys.exit(0)则外壳返回“ OK”

如果我的python脚本调用sys.exit(1)(或任何非零整数),则外壳程序返回“不正确”

了解外壳程序是您的工作,并阅读脚本的文档(或源代码)以了解退出代码的含义。

The answer is “Depends on what exit code zero means”

However, in most cases, this means “Everything is Ok”


I like POSIX:

So, on the shell, I would type:

python script.py && echo 'OK' || echo 'Not OK'

If my python script calls sys.exit(0) the shell returns ‘OK’

If my python script calls sys.exit(1) (or any non-zero integer), the shell returns ‘Not OK’

It’s your job to get clever with the shell, and read the documentation (or the source) for your script to see what the exit codes mean.


回答 6

操作系统命令具有退出代码。查找linux退出代码,以了解有关此内容的一些信息。外壳程序使用退出代码来确定程序是否正常运行,出现问题或失败。已经做了一些努力来创建标准(或至少是常用的)退出代码。请参阅此高级Shell脚本发布。

Operating system commands have exit codes. Look for linux exit codes to see some material on this. The shell uses the exit codes to decide if the program worked, had problems, or failed. There are some efforts to create standard (or at least commonly-used) exit codes. See this Advanced Shell Script posting.


回答 7

我建议使用内置的exit(0)进行干净关机,而不要使用sys.exit()

我不确定这是否是个好建议。

提到的文档非常如此

站点模块(在启动期间会自动导入,除非指定了-S命令行选项)会向内置命名空间添加多个常量。它们对于交互式解释器外壳很有用,不应在程序中使用

然后:

退出(代码=无)

对象,在打印时会打印一条消息,例如“使用quit()或Ctrl-D(即EOF)退出”,并在调用时使用指定的退出代码引发SystemExit。

如果将它与sys.exit()文档进行比较,则它使用的是引发SystemExit异常的相同机制。

I recommend a clean shutdown with the exit(0) builtin rather than using sys.exit()

I’m not sure if this is good advice.

The very documentation mentioned reads:

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.

Then:

exit(code=None)

Objects that when printed, print a message like “Use quit() or Ctrl-D (i.e. EOF) to exit”, and when called, raise SystemExit with the specified exit code.

If we compare it to sys.exit() documentation, it’s using the same mechanism which is raising a SystemExit exception.


回答 8

如果你想移植应用的标准POSIX退出代码,请参阅exitstatusPyPI上的包。

安装软件包:

$ pip install exitstatus

在您的代码中使用:

import sys
from exitstatus import ExitStatus

sys.exit(ExitStatus.success)

If you would like to portably use the standard POSIX exit codes, see the exitstatus package on PyPI.

Install the package:

$ pip install exitstatus

Use in your code:

import sys
from exitstatus import ExitStatus

sys.exit(ExitStatus.success)

回答 9

退出代码仅具有脚本作者指定的含义。Unix的传统是退出代码0表示“成功”,其他都是失败的。确保给定脚本的退出代码意味着什么的唯一方法是检查脚本本身。

The exit codes only have meaning as assigned by the script author. The Unix tradition is that exit code 0 means ‘success’, anything else is failure. The only way to be sure what the exit codes for a given script mean is to examine the script itself.


回答 10

许多编程语言中的退出代码取决于程序员。因此,您必须查看程序源代码(或手册)。零通常表示“一切正常”。

Exit codes in many programming languages are up to programmers. So you have to look at your program source code (or manual). Zero usually means “everything went fine”.


回答 11

为了允许执行异常处理程序和其他操作,我建议使用 exit(0)内置而不要使用sys.exit()它来突然终止进程。

To allow exception handlers and other things execute I recommend a clean shutdown with the exit(0) builtin rather than using sys.exit() which terminates the process abruptly.