title: How to wait for Python subprocess.check_output() to complete?
body: <p>I am running this code in a loop - in Python 3.6:</p>
  
  <pre><code># previous code: copy certain files to the working folder
      shellCmd = "myCmd " + file1 + " " + file2
  
  # myCmd works on file1 and file2  
      result = subprocess.check_output(myCmd, shell=True)
  
  # delete the files
  </code></pre>
  
  <p>Every now and then I get a fail caused by access denied to some of the files. I am guessing that the subprocess is running in the background and the loop continues, spawning other subprocesses. Sometimes this will cause one subprocess trying to copy (or delete) files that myCmd is still busy with in another subprocess. </p>
  
  <p>How do I stop and wait for subprocess.check_output() to complete?  </p>
  
  <p>I saw that subprocess.Popen has a wait() function, but I need the result string from the myCmd process, so want to use subprocess.check_output(). </p>
  
  <p>But any solution that will (a) get me the string output of myCmd and (b) ensure the subprocesses happen in series,  is OK. </p>
  
  <p>Thanks!</p>