kill a task with name containing whitespace using python os.system
<p>From a python script, I am trying to terminate a task.
The task name contains whitespace. </p>

<p>Like "My Program.exe" for instance.</p>

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

<pre><code>os.system("TASKKILL /F /IM My Program.exe")
</code></pre>

<p>I can't find how to escape the whitespace.</p>

<p>I tried</p>

<pre><code>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\'")
</code></pre>

<p>But still does not work.</p>

<p>This worked for me:</p>

<pre><code>os.system("TASKKILL /F /IM \"My Program.exe\"")
</code></pre>

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

python whitespace taskkill