[英]Kill every python process name with psutil except one
I'm using the following python script to kill every process with the given name: 我正在使用以下python脚本杀死具有给定名称的每个进程:
import psutil
for proc in psutil.process_iter():
if proc.name() == "processname":
proc.kill()
I want the script to leave one process with the given name open. 我希望脚本保留给定名称打开的一个进程。 How can I achieve this?
我该如何实现? Is it possibile using this method?
使用这种方法可以吗?
You should simply skip the first one: 您应该只跳过第一个:
piter = psutil.process_iter()
first = True
for proc in psutil.process_iter():
if proc.name() == "processname":
if First:
First = False
else:
proc.kill()
这是另一个可行的解决方案:
[func() for func in [proc.kill for proc in psutil.process_iter() if proc.name()=="processname"][1:]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.