0

I know that in windows cmd I can type the following text to end a process:

taskkill/im chrome.exe /f

But how can I do it using Python?

I've tried this:

from subprocess import *
call("taskkill/im chrome.exe /f", shell=True)

But it showed this:

'taskkill' is not recognized as an internal or external command, operable program or batch file.

So what's wrong with my code? How can I make it work?

hklel
  • 1,624
  • 23
  • 45
  • possible duplicate of [Is it possible to kill a process on Windows from within Python?](http://stackoverflow.com/questions/6278847/is-it-possible-to-kill-a-process-on-windows-from-within-python) – RvdK Jun 25 '14 at 11:10

3 Answers3

0

Try this:

import os

os.system("taskkill/im chrome.exe /f")

I don't have access to a machine with windows here, but it should work.

Edit:

Try also:

1- The command needed a white space between taskkill and /IM, so:

 os.system("taskkill /im chrome.exe /f")

2- Taskkill is not in Windows XP, there ais a less powerfull tool called tskill so:

os.system("tskill chrome")
inigoD
  • 1,681
  • 14
  • 26
  • Thanks for your help, but the same problem occured again. :( – hklel Jun 25 '14 at 10:55
  • Oh, I don't know why but both of the two method didn't work. They just showed 'taskkill'(or tskill) is not recognized as... – hklel Jun 25 '14 at 11:10
0

As mentioned in the question linked to by RvdK, above: you need a fully-qualified path to taskkill.exe, but not mentioned was the fact that you need to remember to escape the backslashes, so

call("c:\\windows\\system32\\taskkill.exe /im chrome.exe /f", shell=True)

Should work (it does on my Win7 machine).

Note that you don't actually need the shell=True bit, as mentioned in the subprocess docs:

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

So call("c:\\windows\\system32\\taskkill.exe /im chrome.exe /f") Is better.

SiHa
  • 7,830
  • 13
  • 34
  • 43
0

#my memo

from win32com.client import GetObject
import os

WMI = GetObject('winmgmts:')
for p in WMI.ExecQuery('select * from Win32_Process where Name LIKE "%chrome%"'):
    os.system("taskkill /F /PID "+str(p.ProcessId))
mariko
  • 71
  • 1
  • 3