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?

1

3 Answers 3

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")
2
  • Thanks for your help, but the same problem occured again. :(
    – hklel
    Jun 25, 2014 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, 2014 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.

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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.