title: Wait for external program to finish writing to file
body: <p>I have a Python script that calls an external program (<code>sox</code> to be precise). Now I have to do several things with <code>sox</code>, but always have to wait until one file is done writing so I can use it as an input file in my next command.</p>
  
  <p><code>subprocess.wait()</code> doesn't work, because the execution of <code>sox</code> will be done, but the file won't be done writing.</p>
  
  <p>Here is the code I have:</p>
  
  <pre><code>import tempfile
  import shlex
  
  file_url = '/some/file.wav'
  out_file = 'some/out.wav'
  tmp_file = tempfile.NamedTemporaryFile()
  pad_cmd = 'sox ' + file_url + ' ' + tmp_file.name + ' pad 0.0 3.0'
  subprocess.call(shlex.split(pad_cmd))
  trim_cmd = 'sox ' + tmp_file.name + ' -t wav ' + out_file + ' trim 0.0 3.0'
  </code></pre>
  
  <p>Any way to wait for the file to finish writing to <code>tmp_file.name</code> without using a timer that waits for a fixed amount of time. Or is there a built-in way in <code>sox</code> to use the output file as input? The special filename <code>-</code> didn't work for <code>pad</code> and <code>trim</code>.</p>