Is it possible to kill a process on Windows from within Python?

cupof picture cupof · Jun 8, 2011 · Viewed 91.6k times · Source

I'm using Python 2.6. Sometimes there become several instances of a certain process open, and that process causes some problems in itself. I want to be able to programatically detect that there are multiple instances of that process and to kill them.

For example, maybe in some cases there are 50 instances of make.exe open. I want to be able to tell that there are 20 instances open, and to kill them all. How is this accomplished?

Answer

Nix picture Nix · Jun 8, 2011

I would think you could just use taskkill and the Python os.system()

import os
os.system("taskkill /im make.exe")

Note: I would just note you might have to fully qualify the taskkill path. I am using a Linux box so I can't test...