End the method of running python

Sometimes when a condition is established, the program needs to be terminated. You can use sys.exit() to exit the program. sys.exit() will raise an exception

  1. If this exception is not caught, then the python compiler will exit and the following program will not be executed.

  2. If this exception is caught (try...except...finally), catching this exception can do some extra cleanup work, and the subsequent program will continue to execute.

Note: 0 means normal exit, other values (1-127) are abnormal, and abnormal events can be thrown for capture.

Another way to terminate the program os._exit()

In general, use sys.exit(), generally use os._exit() in the child process from fork

import os, sys
import pandas as pd
import numpy as np
df=pd.DataFrame({'a':[1,2,3,4],'b':['a','b','c',np.nan],'c':['2017-09','2017-09-12','2017-08-22','2017-07-11'],'d':['2017-09','2017-12','2017-08','2017-07']})
t=df.iloc[2,:].tolist()
df.columns=t
print(df)
c=[1,2,5]
dic={1:2,2:3,3:4}
# print(dic.keys())
new_col=[]for x in c:if x not in dic.keys():
 new_col.append(x)if new_col:print(new_col)
 sys.exit(1)print('ssss')

Knowledge point expansion:

1. sys.exit()

Executing this statement will directly exit the program, which is also a frequently used method, and does not need to consider the influence of factors such as the platform. It is generally the preferred method to exit the Python program.

This method contains a parameter status, which is 0 by default, which means normal exit, or 1, which means abnormal exit.

import sys
sys.exit()
sys.exit(0)
sys.exit(1)

This method raises a SystemExit exception (this is the only exception that will not be considered an error). When there is no setting to capture this exception, the program execution will exit directly. Of course, this exception can also be caught to perform some other operations.

2. os._exit()

The effect is also to exit directly without throwing an exception, but its use will be restricted by the platform, but our commonly used Win32 platform and UNIX-based platforms will not be affected.

It is said on Zhihu that the _exit() function of the C language is called (not verified)

3. os.kill()

Generally used to kill the process directly, but it can only be effective on the UNIX platform.

Basic principle: This function simulates the traditional UNIX function to send signals to the process, which contains two parameters: one is the process name, which is the process to receive the signal; the other is the operation to be performed.

So far this article on the method of ending running python is introduced. For more information about how to stop running python, please search the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future!

Recommended Posts

End the method of running python
The specific method of python instantiation object
The specific method of python import library
Detailed explanation of the principle of Python super() method
Solve the problem of python running startup error
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Python method of parameter passing
Consolidate the foundation of Python (3)
The usage of wheel in python
Summary of logarithm method in Python
Python simulation of the landlord deal
What is the use of Python
Python implements the steepest descent method
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
Understanding the meaning of rb in python
Learn the basics of python interactive mode
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Where is the pip path of python3
Method of installing django module in python
The essence of Python language: Itertools library
python implements the gradient method python the fastest descent method
What are the advantages of python language
Example method of reading sql from python
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
Solve the conflict of multiple versions of python
7 features of Python3.9
2.1 The Python Interpreter (python interpreter)
Method analysis of Python calling C language program
What is the function of adb in python
The difference between the syntax of java and python
Python realizes the development of student management system
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Can the value of the python dictionary be modified?
Python implements the source code of the snake game
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
The consequences of uninstalling python in ubuntu, very
The latest method of installing Mongodb on Ubuntu 16.04
Python writes the game implementation of fishing master
Python magic method topic
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification
What is the advantage of python over corporate language