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

Python

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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().

Solution 2 - Python

os._exit calls the C function _exit() which does an immediate program termination. Note the statement "can never return".

sys.exit() is identical to raise SystemExit(). It raises a Python exception which may be caught by the caller.

Original post: http://bytes.com/topic/python/answers/156121-os-_exit-vs-sys-exit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAamir RindView Question on Stackoverflow
Solution 1 - PythonNPEView Answer on Stackoverflow
Solution 2 - PythonprelicView Answer on Stackoverflow