Python exit commands: quit(), exit(), sys.exit() and os._exit()
The functions quit(), exit(), sys.exit() and os._exit() have almost same functionality as they raise the SystemExit exception by which the Python interpreter exits and no stack traceback is printed.
We can catch the exception to intercept early exits and perform cleanup activities; if uncaught, the interpreter exits as usual.
When we run a program in Python, we simply execute all the code in file, from top to bottom. Scripts normally exit when the interpreter reaches the end of the file, but we may also call for the program to exit explicitly with the built-in exit functions.
- quit()
It works only if the site module is imported so it should not be used in production code. Production code means the code is being used by the intended audience in a real-world situation. This function should only be used in the interpreter.
It raises the SystemExit exception behind the scenes. If you print it, it will give a message:
Example:
# Python program to demonstrate# quit()foriinrange(10):# If the value of i becomes# 5 then the program is forced# to quitifi==5:# prints the quit messageprint(quit)quit()print(i)chevron_rightfilter_noneOutput:
0 1 2 3 4 Use quit() or Ctrl-D (i.e. EOF) to exit
- exit()
exit()is defined insite.pyand it works only if the site module is imported so it should be used in the interpreter only. It is like a synonym ofquit()to make the Python more user-friendly. It too gives a message when printed:Example:
# Python program to demonstrate# exit()foriinrange(10):# If the value of i becomes# 5 then the program is forced# to exitifi==5:# prints the exit messageprint(exit)exit()print(i)chevron_rightfilter_noneOutput:
0 1 2 3 4 Use exit() or Ctrl-D (i.e. EOF) to exit
- sys.exit([arg])
Unlike
quit()andexit(),sys.exit()is considered good to be used in production code for the sys module is always available. The optional argumentargcan be an integer giving the exit or another type of object. If it is an integer, zero is considered “successful termination”.Note: A string can also be passed to the sys.exit() method.
Example – A program which stops execution if age is less than 18.
# Python program to demonstrate# sys.exit()importsysage=17ifage <18:# exits the programsys.exit("Age less than 18")else:print("Age is not less than 18")chevron_rightfilter_noneOutput:
An exception has occurred, use %tb to see the full traceback. SystemExit: Age less than 18
- os._exit(n)
os._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 issys.exit(n)method.# Python program to explain os._exit() method# importing os moduleimportos# Create a child process# using os.fork() methodpid=os.fork()# pid greater than 0# indicates the parent processifpid >0:print("\nIn parent process")# Wait for the completion# of child process and# get its pid and# exit status indication using# os.wait() methodinfo=os.waitpid(pid,0)# os.waitpid() method returns a tuple# first attribute represents child's pid# while second one represents# exit status indication# Get the Exit code# used by the child process# in os._exit() method# firstly check if# os.WIFEXITED() is True or notifos.WIFEXITED(info[1]) :code=os.WEXITSTATUS(info[1])print("Child's exit code:", code)else:print("In child process")print("Process ID:", os.getpid())print("Hello ! Geeks")print("Child exiting..")# Exit with status os.EX_OK# using os._exit() method# The value of os.EX_OK is 0os._exit(os.EX_OK)chevron_rightfilter_noneOutput:
In child process Process ID: 25491 Hello ! Geeks Child exiting.. In parent process Child's exit code: 0
Among above four exit functions, sys.exit() is preferred mostly, because the exit() and quit() functions cannot be used in production code while os._exit() is for special cases only when immediate exit is required.
Recommended Posts:
- Python Exit handlers (atexit)
- wxPython | Exit() function in wxPython
- How to use close() and quit() method in Selenium Python ?
- quit driver method - Selenium Python
- Python | Execute and parse Linux commands
- Python - Read blob object in python using wand library
- twitter-text-python (ttp) module - Python
- Python | Index of Non-Zero elements in Python list
- Python | PRAW - Python Reddit API Wrapper
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- MySQL-Connector-Python module in Python
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


