大侠请留步!python怎么杀死windows进程
大侠请留步!!!python如何杀死windows进程
例:程序A
怎样实现判断“程序A”有没有运行?如果运行就杀死!
------解决方案--------------------
例:程序A
怎样实现判断“程序A”有没有运行?如果运行就杀死!
------解决方案--------------------
- Python code
import os import time ret = 1 while(1): if ret==0: print u'目标进程存在,杀死该进程' ret = 1 continue else: print u'目标进程不存在' print time.time() ret = os.system('tasklist | find "QQ.exe"') print ret print '-'*50 time.sleep(5)
------解决方案--------------------
美女程序辕,
- Python code
#------------------------ #--- using os module import os os.system("taskkill /im /f") here /f is used to kill forcefully. /im is for name of the file #------------------------ #--- using subprocess import subprocess handle = subprocess.Popen("", shell=False) subprocess.Popen("taskkill /F /T /PID %i"%handle.pid , shell=True) #------------------------ #--- using subprocess # Create a process that won't end on its own import subprocess process = subprocess.Popen(['', '-c', 'while 1: pass']) #------------------------ #--- using pywin32 import win32api PROCESS_TERMINATE = 1 handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid) win32api.TerminateProcess(handle, -1) win32api.CloseHandle(handle) #------------------------ #--- using ctypes import ctypes PROCESS_TERMINATE = 1 handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid) ctypes.windll.kernel32.TerminateProcess(handle, -1) ctypes.windll.kernel32.CloseHandle(handle) note : ctypes solution is only available to you if you are using Python 2.3 or higher. #------------------------ #--- using wmi import wmi c = wmi.WMI() pidList = [] for process in c.Win32_Process (caption=""): pid = process.ProcessId for process in c.Win32_Process (ProcessId=pid): process.Terminate () #------------------------