Python exit commands - why so many and when should each be used?

0 votes

It seems that python supports many different commands to stop script execution.

The choices I've found are: 
quit()
exit()
sys.exit()
os._exit()

Have I missed any? What's the difference between them? When would you use each?
Aug 27, 2018 in Python by bug_seeker
• 15,300 points
422 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Let me give some information on them:

  1. quit raises the SystemExit exception behind the scenes.

  2. Furthermore, if you print it, it will give a message:

  3. >>> print (quit) Use quit() or Ctrl-Z plus Return to exit >>>

  4. This functionality was included to help people who do not know Python. After all, one of the most likely things a newbie will try to exit Python is typing in quit.

  5. Nevertheless, quit should not be used in production code. This is because it only works if the site module is loaded. Instead, this function should only be used in the interpreter.

  6. exit is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly.

  7. Furthermore, it too gives a message when printed:

  8. >>> print (exit) Use exit() or Ctrl-Z plus Return to exit >>>

  9. However, like quit, exit is considered bad to use in production code and should be reserved for use in the interpreter. This is because it too relies on the site module.

  10. sys.exit raises the SystemExit exception in the background. This means that it is the same as quit and exit in that respect.

  11. Unlike those two however, sys.exit is considered good to use in production code. This is because the sys module will always be there.

  12. os._exit exits the program without calling cleanup handlers, flushing stdio buffers, etc. Thus, it is not a standard way to exit and should only be used in special cases. The most common of these is in the child process(es) created by os.fork.

  13. Note that, of the four methods given, only this one is unique in what it does.

Summed up, all four methods exit the program. However, the first two are considered bad to use in production code and the last is a non-standard, dirty way that is only used in special scenarios. So, if you want to exit a program normally, go with the third method: sys.exit.
Or, even better in my opinion, you can just do directly what
sys.exit does behind the scenes and run:

raise SystemExit

This way, you do not need to import sys first.

However, this choice is simply one on style and is purely up to you.

answered Aug 27, 2018 by Priyaj
• 56,140 points

Related Questions In Python

0 votes
1 answer

Python ctypes segmentation fault when rootfs is read-only and /tmp is noexec

I cant really seem to reproduce the ...READ MORE

answered Aug 27, 2018 in Python by anonymous
47 views
0 votes
1 answer

When I create and remove files rapidly on windows using python I get WindowsError (Error 5)

Here's the short answer: disable any antivirus or ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
76 views
0 votes
1 answer

Which IDE can be used for Python on Mac OS X

Have tried many different (Kate, Eclipse, Scite, ...READ MORE

answered Oct 15, 2018 in Python by SDeb
• 11,020 points
42 views
0 votes
1 answer

What is absolute import in Python and how is it used?

An absolute {import, path, URL} tells you exactly how ...READ MORE

answered Nov 30, 2018 in Python by Nymeria
• 3,500 points

edited Dec 10, 2018 by Nymeria 61 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
63 views
+4 votes
6 answers
0 votes
1 answer

Whenever you exit Python, is all memory de-allocated?

The answer here is no. The modules ...READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 56,140 points
18 views
0 votes
1 answer

*args and **kwargs in python

In cases when we don’t know how ...READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 56,140 points
19 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.