首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

大侠请留步!python怎么杀死windows进程

2012-10-10 
大侠请留步!!!python如何杀死windows进程例:程序A怎样实现判断“程序A”有没有运行?如果运行就杀死![解决办

大侠请留步!!!python如何杀死windows进程
例:程序A
怎样实现判断“程序A”有没有运行?如果运行就杀死!

[解决办法]

Python code
import osimport timeret = 1while(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 moduleimport osos.system("taskkill /im /f")here /f is used to kill forcefully./im is for name of the file#------------------------#--- using subprocessimport subprocesshandle = 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 ownimport subprocessprocess = subprocess.Popen(['', '-c', 'while 1: pass'])#------------------------#--- using pywin32import win32apiPROCESS_TERMINATE = 1handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)win32api.TerminateProcess(handle, -1)win32api.CloseHandle(handle)#------------------------#--- using ctypesimport ctypesPROCESS_TERMINATE = 1handle = 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 wmiimport wmic = wmi.WMI()pidList = []for process in c.Win32_Process (caption=""): pid = process.ProcessId for process in c.Win32_Process (ProcessId=pid):process.Terminate ()#------------------------ 

热点排行