title: Is there a foolproof way to give the system enough time to delete a folder before running copytree
body: <p>I have some directories that I need to update I was using the following code</p>
  
  <pre><code>for newdir in newdirs:
      olddir = newdir.replace('h:\\','G:\\').replace('_academic','')
      shutil.rmtree(newdir)
      shutil.copytree(olddir,newdir)
  </code></pre>
  
  <p>I would occasionally get an error</p>
  
  <pre><code>WindowsError: [Error 5] Access is denied: 'h:\\mydir\\sub1\\sub2\\sub3\\sub4\\sub5'
  </code></pre>
  
  <p>since the error would not happen on the previous directories I decided that the cause must be some access conflict - too little time was taking place between the rmtree call and copytree so I modified the code to waste some time </p>
  
  <pre><code>for newdir in newdirs:
      olddir = newdir.replace('h:\\','G:\\').replace('_academic','')
      shutil.rmtree(newdir)
      for item in range(0,20,1):
          pass
      shutil.copytree(olddir,newdir)
  </code></pre>
  
  <p>This made the error go away and the old directory was copied to the new location.</p>
  
  <p>I don't like this because it seems pretty ramshackle - even for me.  </p>
  
  <ol>
  <li>Am I wrong about the cause of the error (conflict)</li>
  <li>Is there a better way to make sure the system has enough time to complete the rmtree operation before we begin the copytree?</li>
  </ol>