Python: waiting for external launched process finish

Grigor Gevorgyan picture Grigor Gevorgyan · Jan 21, 2012 · Viewed 64.7k times · Source

The question already in title - how can one make the python script wait until some process launched with os.system() call is completed ? For example a code like

    for i in range( 0, n ):
       os.system( 'someprog.exe %d' % i )

This launches the requested process n times simultaneously, which may make my pc to sweat a bit )

Thanks for any advice.

Answer

Dor Shemer picture Dor Shemer · Jan 21, 2012

Use subprocess instead:

import subprocess
for i in xrange(n):
  p = subprocess.Popen(('someprog.exe', str(i))
  p.wait()

Read more here: http://docs.python.org/library/subprocess.html