简体   繁体   中英

Kill every python process name with psutil except one

I'm using the following python script to kill every process with the given name:

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:]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM