When you go to Task Manager and select an app and press "End Task", the program closes. How can I do this process in python? So when the code runs, for example, the Chrome app closes.
Asked
Active
Viewed 766 times
0
-
Start by reviewing the *psutil* module. From there you will be able to identify the process you want to kill. Then study the *os* module which supports a kill function – DarkKnight Mar 02 '22 at 15:56
-
`os.system(f'taskkill /F /PID {pid_number}')` where pid_number is a variable containing the pid of the process you want to kill – Mar 02 '22 at 15:57
-
hey sembei , good work , it worked. but some of the programs do not have a PID number. like chrome and etc. – Parsa Ad Mar 02 '22 at 16:12
2 Answers
0
Clone of No.6278847: Is it possible to kill a process on Windows from within Python?
You can do that through the following code:
import os
def terminate(ProcessName):
os.system('taskkill /im ' + ProcessName)
terminate('chrome.exe')
Credits: Nix
-
-
Oh alright, I am new to stack overflow, will mind that from next time. Thanks! – Danny Mar 02 '22 at 16:05
-
1so , what i need to place on the "processname"? like "chrome" or "chrome.exe" or full path? – Parsa Ad Mar 02 '22 at 16:08
-
I believe just 'chrome.exe' would do, if there is an exception, try specifying the full path. – Danny Mar 02 '22 at 16:14
0
Couple of links related to this: (1) kill process using os package (2) Kill process using wmi package in windows

Karthikeyan S
- 32
- 4