Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between sys.exit(0) and os._exit(0)

Tags:

python

Please help me in clarifying the concept of these two python statements in terms of difference in functionality:

  1. sys.exit(0)

  2. os._exit(0)

like image 332
Aamir Rind Avatar asked Mar 06 '12 20:03

Aamir Rind


People also ask

What does OS _exit do in Python?

_exit() method in Python is used to exit the process with specified status without calling cleanup handlers, flushing stdio buffers, etc. Note: This method is normally used in child process after os. fork() system call. The standard way to exit the process is sys.

What is the difference between SYS exit and exit?

exit is a helper for the interactive shell - sys. exit is intended for use in programs. 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 (e.g. exit ).

When should I use sys exit?

exit is generally used in programs to raise the SystemExit Exception. With this exception, the program is closed with a proper code passed into the argument.

What does exit 0 mean in Python?

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.


1 Answers

According to the documentation:

os._exit(): 

Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.

Note The standard way to exit is sys.exit(n). _exit() should normally only be used in the child process after a fork().

like image 135
NPE Avatar answered Oct 20 '22 00:10

NPE