Killing processes with PSUTIL

后端 未结 5 1467
陌清茗
陌清茗 2021-01-12 07:58

I\'m looking to write some code that will kill off a process based on it\'s name and who owns it. This works fine on Windows XP but when I come to run the same code on Windo

5条回答
  •  花落未央
    2021-01-12 08:24

    the problem is some of the processes does not have names, and therefor you get the error.

    If you put that in a try block it will work:

    PROCNAME = 'python.exe'
    for proc in psutil.process_iter():
      try:
          if proc.name == PROCNAME:
          p = psutil.Process(proc.pid)
    
          etc..
      except:
        pass
    

提交回复
热议问题