title: Kill all python main processes apart from current one
body: <p>Consider the following script:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  plt.plot([1,2,3], [1,2,3])
  plt.show()
  </code></pre>
  
  <p>Everytime you run it, it creates a new figure within a new <strong>python.exe</strong> process, if you don't close the figure before. But I want to close all previous open figures (it's just an example, please no matplotlib solutions), means all previous opened processes.</p>
  
  <p>This is my approach:</p>
  
  <ol>
  <li>get current process ID with <code>os</code></li>
  <li>get all process IDs related to <strong>python</strong> with <code>psutil</code></li>
  <li>filter out current ID from all python IDs</li>
  <li>kill remaining list of IDs</li>
  </ol>
  
  <hr>
  
  <pre><code>import os
  currentId = os.getpid()
  
  import psutil
  allPyIds = [p.pid for p in psutil.process_iter() if "python" in str(p.name)]
  
  PyIdsToKill = [x for x in allPyIds if x != currentId]
  for PyId in PyIdsToKill:
      os.kill(PyId, 1)
  </code></pre>
  
  <p>It works, it closes all open python processes apart from the current one.
  However I get the following error, when there are actually processes to close:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):   File "C:....py", line 10, in
    
        for PyId in PyIdsToKill: OSError: [WinError 87] Falscher Parameter [Finished in 0.3s with exit code 1]</p>
  </blockquote>
  
  <p><strong>What is my mistake?</strong></p>
  
  <hr>
  
  <p>I'm running on Windows 7 Pro:</p>
  
  <blockquote>
    <p>Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600
    64 bit (AMD64)] on win32</p>
  </blockquote>