Milo Milo - 1 year ago 107
Python Question

kill a task with name containing whitespace using python os.system

From a python script, I am trying to terminate a task.
The task name contains whitespace.

Like "My Program.exe" for instance.

I use the following line but it complains about invalid argument because the name contains a whitespace.

os.system("TASKKILL /F /IM My Program.exe")

I can't find how to escape the whitespace.

I tried

os.system("TASKKILL /F /IM "My Program.exe"")
os.system("TASKKILL /F /IM \"My Program.exe\"")
os.system("TASKKILL /F /IM 'My Program.exe'")
os.system("TASKKILL /F /IM \'My Program.exe\'")

But still does not work.

Answer Source

This worked for me:

os.system("TASKKILL /F /IM \"My Program.exe\"")

Check if your application is really named 'My Program.exe' on task manager. Also, check if you have privileges to be killing that process.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download