"""
获取所有进程
"""
def get_process():
#获取当前所有的进程
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
process_name = p.name()
print("Process name is: %s, pid is: %s" % (process_name, pid))

"""
通过进程名判断该进程是否存在
"""
def isExist_Process(processname):
print("待查找的进程:"+processname)
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
if p.name() == processname:
print("存在进程:"+processname)
break
else:
print("not found")

"""
通过进程名杀死进程
taskkill /F /IM explorer.exe
"""
def kill_name(name):
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
if p.name() == name:
cmd = r'taskkill /F /IM '+ name
os.system(cmd)

"""
通过进程名启动一个进程
"""
def start_Process(name):
os.system(name)
"""
根据exe文件名启动一个进程(非系统exe须带路径)
"""
import win32api
import psutil

def exec_process(exe_dir,exe_name):
win32api.ShellExecute(0, 'open', exe_dir, '', '', 1)
pids = psutil.pids()
for pid in pids:
if psutil.Process(pid).name() == exe_name:
return {"pid":pid}

#杀死一个进程
def kill_process(process_name):
pids = psutil.pids()
for pid in pids:
if psutil.Process(pid).name() == process_name:
cmd = "wmic process where name='%s' call terminate" % process_name
os.system(cmd)
return True
#ret = os.system('tasklist | find "Calculator.exe"')
#print(ret)
# if ret:
# # 关闭进程
# os.system('taskkill /F /IM TabTip.exe')
# #ret = os.popen('tasklist | find "TabTip.exe"')