title: os.mkdir can fail with PermissionError after successful shutil.rmtree on Windows
body: <p>Consider the following python function for cleaning a directory:</p>
  
  <pre><code>def cleanDir(path):
    shutil.rmtree(path)
    os.mkdir(path)
  </code></pre>
  
  <p>On Windows (actually tested on Windows7 and Windows10 with python 2.7.10 and 3.4.4), when navigating at the same time with Windows Explorer into the corresponding directory (or when only navigating in the left tree pane to the parent folder), the following exception may be raised:</p>
  
  <pre><code>Traceback (most recent call last):
    ...
    File "cleanDir.py", line ..., in cleanDir
      os.mkdir(path)
  PermissionError: [WinError 5] Access is denied: 'testFolder'
  </code></pre>
  
  <p>The problem has already been reported in this <a href="https://stackoverflow.com/questions/49809471">issue</a>. But it was not analyzed further and the given solution using sleep is not satisfying. According to Eryk's comments below the same behaviour is also to be expected up to current python versions, i.e. python 3.8.</p>
  
  <p>Note that <code>shutil.rmtree</code> returns without exception. But trying to create the directory again immediately may fail. (A retry is most of the time successful, see the full code for testing below.) And note that you need to click around in the Windows Explorer in the test folders, left and right side, to force the problem.</p>
  
  <p>The problem seems to be in the Windows filesystem API functions (and not in the Python <code>os</code> module): deleted folders seem not to be "forwarded" to all functions immediately, when Windows Explorer has a handle on the corresponding folder.</p>
  
  <pre><code>import os, shutil
  import time
  
  def populateFolder(path):
    if os.path.exists(path):
      with open(os.path.join(path,'somefile.txt'), 'w') as f:
        f.write('test')
    #subfolderpath = os.path.join(path,'subfolder')
    #os.mkdir(subfolderpath)
    #with open(os.path.join(subfolderpath,'anotherfile.txt'), 'w') as f2:
    #  f2.write('test')
  
  def cleanDir(path):
    shutil.rmtree(path)
    os.mkdir(path)
  
  
  def cleanDir_safe(path):
    shutil.rmtree(path)
  
    try:
      #time.sleep(0.005) # makes first try of os.mkdir successful
      os.mkdir(path)
    except Exception as e:
      print('os.mkdir failed: %s' % e)
      time.sleep(0.01)
      os.mkdir(path)
  
    assert os.path.exists(path)
  
  
  FOLDER_PATH = 'testFolder'
  if os.path.exists(FOLDER_PATH):
    cleanDir(FOLDER_PATH)
  else:
    os.mkdir(FOLDER_PATH)
  
  loopCnt = 0
  while True:
    populateFolder(FOLDER_PATH)
    #cleanDir(FOLDER_PATH)
    cleanDir_safe(FOLDER_PATH)
    time.sleep(0.01)
    loopCnt += 1
    if loopCnt % 100 == 0:
      print(loopCnt)
  </code></pre>