请问python杀死windows进程
请教python杀死windows进程
最近在学习python方面的知识,根据这门语言很简洁,挺有意思的,这里想请教大家一个问题。
请教下在python中如何杀掉windows中的进程,根据进程名获取进程的ID,我的版本是2.7,没有psutil模块,请教大家下如何实现的,谢谢!
------解决方案--------------------
别人写的,转一下:
最近在学习python方面的知识,根据这门语言很简洁,挺有意思的,这里想请教大家一个问题。
请教下在python中如何杀掉windows中的进程,根据进程名获取进程的ID,我的版本是2.7,没有psutil模块,请教大家下如何实现的,谢谢!
------解决方案--------------------
别人写的,转一下:
- Python code
import ctypes import sys TH32CS_SNAPPROCESS = 0x00000002 class PROCESSENTRY32(ctypes.Structure): _fields_ = [("dwSize", ctypes.c_ulong), ("cntUsage", ctypes.c_ulong), ("th32ProcessID", ctypes.c_ulong), ("th32DefaultHeapID", ctypes.c_ulong), ("th32ModuleID", ctypes.c_ulong), ("cntThreads", ctypes.c_ulong), ("th32ParentProcessID", ctypes.c_ulong), ("pcPriClassBase", ctypes.c_ulong), ("dwFlags", ctypes.c_ulong), ("szExeFile", ctypes.c_char * 260)] def getProcList(): CreateToolhelp32Snapshot = ctypes.windll.kernel32.CreateToolhelp32Snapshot Process32First = ctypes.windll.kernel32.Process32First Process32Next = ctypes.windll.kernel32.Process32Next CloseHandle = ctypes.windll.kernel32.CloseHandle hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) pe32 = PROCESSENTRY32() pe32.dwSize = ctypes.sizeof(PROCESSENTRY32) if Process32First(hProcessSnap,ctypes.byref(pe32)) == False: print >> sys.stderr, "Failed getting first process." return while True: yield pe32 if Process32Next(hProcessSnap,ctypes.byref(pe32)) == False: break CloseHandle(hProcessSnap) def getChildPid(pid): procList = getProcList() for proc in procList: if proc.th32ParentProcessID == pid: yield proc.th32ProcessID def killPid(pid): childList = getChildPid(pid) for childPid in childList: killPid(childPid) handle = ctypes.windll.kernel32.OpenProcess(1, False, pid) ctypes.windll.kernel32.TerminateProcess(handle,0) if __name__ =='__main__': args = sys.argv if len(args) >1 : pid = int(args[1]) killPid(pid) else: procList = getProcList() for proc in procList: print proc.szExeFile+' '+str(proc.th32ParentProcessID) + ' '+str(proc.th32ProcessID) #---------------------- # # Usage demo # #---------------------- import subprocess import time #import winproc timeout = 2 process = subprocess.Popen("cmd /k ping localhost -t",shell = True) start = int(time.time()) while process.poll()==None: now = int(time.time()) if int (now - start) >timeout: pid = process.pid break winproc.killPid(pid) print "End"
------解决方案--------------------
- Python code
import subprocess import time import wmi c = wmi.WMI () # # Kill a process by id # notepad = subprocess.Popen (["notepad.exe"]) time.sleep (1) for process in c.Win32_Process (ProcessId=notepad.pid): process.Terminate () # # Which process ids correspond to an .exe # for i in range (5): subprocess.Popen (["notepad.exe"]) for process in c.Win32_Process (caption="notepad.exe"): print process.ProcessId # # _Some_ (but not all) of the information about each file # for process in c.Win32_Process (caption="notepad.exe"): print process process.Terminate ()
------解决方案--------------------
- Python code
import os command = 'taskkill /F /IM QQ.exe' #比如杀死QQ进程 os.system(command)