title: os.remove acting weird by removing the contents of the file, but not the file itself
body: <p>I'm writing a web scraper that has to download 8000 files in total. In my script i download the files consecutively and delete the previous one after the relevant information has been extracted. To delete the file, i use "os.remove(downloaded_file)". So far, in 500+ downloads, 3 times it didnt remove the file, but just removed the contents of the file, so an exception happened when the script tried to copy things from an empty file. Has anyone experienced this or can explain what is happening? 
  Working on windows 10</p>
  
  <p>I couldn't fine any relevant information on this error so far. </p>
  
  <pre class="lang-py prettyprint-override"><code>def copy_to_master_and_delete_df(downloaded_file,master_file):
      '''open a downloaded csv file, copy the data (line 10), append to master file and delete the downloaded file'''
      while not os.path.exists(downloaded_file):
          time.sleep(0.5)
          log(f'waiting for {bank} {quarter} to download')
      with open(downloaded_file, encoding='utf-8') as df:
          data = list(df.readlines())[-1]
      os.remove(downloaded_file)
      while os.path.exists(downloaded_file):
          time.sleep(0.1)
          log(f'waiting for {bank} {quarter} to be deleted')
      with open(master_file, 'a', encoding='utf-8') as mf:
          mf.write(data)
  </code></pre>
  
  <p>On data = list(df.readlines())[-1] it gives an exception:</p>
  
  <pre><code>Exception has occurred: IndexError
  list index out of range
  </code></pre>
  
  <p>It happens because of what is described earlier, the contents are removed, but not the file itself. 
  To work around this problem a little, i have put an infinite </p>
  
  <pre class="lang-py prettyprint-override"><code>    while os.path.exists(downloaded_file):
          time.sleep(0.1)
          log(f'waiting for {bank} {quarter} to be deleted')
  </code></pre>
  
  <p>that allows me to manually delete the file and have the script not break down.</p>
  
  <p>I'm asking for help because it went to a next level. The script somehow jumped over the line where i check if the file  is deleted (again, the content is deleted but the file is not) and downloaded the next one, so the script crashed when it looked into an empty file.
  Any ideas to why this is happening or how to handle this?</p>