Tutorial Example

Ways to Exit or Quit a Python Script: A Completed Guide – Python Tutorial

There are some ways to quit or exit a python script. In this tutorial, we will introcude you these ways. As a python beginner, you can learn some very useful tips from this tutorial.

sys.exit(status = 0)

This function can make a python script be terminated, the status can be:

0: quit normally.

1: quit with some exceptions occured.

This function works fine on windows, unix ,linux and mac system.

Here is an example:

import sys
sys.exit(0)

os._exit()

The functin only works fine on windows and unix system. It also can terminate a python script.

Here is an example:

import os
os._exit()

os.kill()

This function can kill a process to stop an apllication and it only works fine on unix sytem.

Syntax

os.kill(pid, signal)

Parameters explained

pid: the process pid

signal: it can be signal.SIGINT, signal.SIGTERM, signal.SIGKILL and signal.SIGALRM

Here is an example:

import os
import singal
os.kill(2294, singal.SIGKILL)