85

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)

Alois Mahdal
  • 10,043
  • 7
  • 49
  • 69
Aamir Rind
  • 36,955
  • 19
  • 118
  • 157

2 Answers2

87

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

NPE
  • 464,258
  • 100
  • 912
  • 987
47

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

prelic
  • 4,260
  • 4
  • 33
  • 45