#!/usr/bin/env python
# coding: utf-8
import os
import psutil
# 如果未知路径且写入了配置环境
# os.system("taskkill /f /im excel.exe & taskkill /f /im wps.exe")
# cmd taskkill 直接输入 不需加双引号 psutil 不能加双引号
# cmd taskkill 无法根据绝对路径关闭程序 无论有没有双引号(无效查询 或 没有找到进程)
# 此种for循环, 不能else return
# 如果for遍历存在符合条件, return, 且不再执行for循环后面的外部程序
# pid 进程id
# 信号编号==9 强制结束
def fun_kill(path):
if os.path.exists(path):
for proc in psutil.process_iter(['pid', 'name', 'exe']):
if proc.info['exe'] == path:
print(proc)
print(path, "has been found, pid:", proc.info['pid'])
os.kill(proc.info['pid'], 9)
print(path, "process has been killed, pid:", proc.info['pid'])
return True
print(path, "is not running")
return True
else:
print(path, "does not exist")
return False
#!/usr/bin/env python
# coding: utf-8
def kill_sap():
import fun_kill
result = fun_kill.fun_kill(path)
print("关闭sap", result)
import read_config
dic_config = read_config.main()
print("dic_config:", dic_config)
path = dic_config["table_path"]["path_5"]
print("sap路径:", path)
if __name__ == "__main__":
kill_sap()