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