title: How to know if an external process has finished in python?
body: <p>I am working from a windows platform. In my python script, I can make a call to an external program in the following way:</p>
  
  <pre><code>os.system("C:\mainfolder\menu.exe C:\others\file1.inp C:\others\file2.inp")
  
  os.popen("C:\mainfolder\menu.exe C:\others\file1.inp C:\others\file2.inp")
  
  subprocess.call(["C:\mainfolder\menu.exe","C:\others\file1.inp" "C:\others\file2.inp"])
  </code></pre>
  
  <p>where:</p>
  
  <p>menu.exe: is my external program.</p>
  
  <p>file1 and file2: are input files to my external program.</p>
  
  <p>All the above works fine. Now that my external program has finished successfully, I need to totally close it along with all the windows that are left opened by it. I have gone through lots of other posts, python documentation, etc and found commands as for example:</p>
  
  <pre><code>os.system("taskkill /im C:\mainfolder\menu.exe")
  
  os.kill(proc.pid,9)
  
  child.kill() 
  </code></pre>
  
  <p>But they did not work. I spent a lot of time trying to find something that worked for me, until I realised that no matter which commands I type after, they will not be read as the computer does not know that my external program has finished. That is the reason why I can easily terminate the program from the command line anytime just by typing taskkill /im menu.exe, but not from python.</p>
  
  <p>Does anybody know how to sort this out?, should I include something else when I make the call to my external program?</p>