title: os.remove - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
body: <p>[Python]
  I try many suggestions in other posts and could not solve the PermissionError: [WinError 32] The process cannot access the file because it is being used by another process</p>
  <pre><code>import os
  from shutil import copyfile
  
  
  LogFile = 'CCH Prepare Delta.txt'
  
  if os.path.exists(LogFile):
      lastmod = datetime.datetime.fromtimestamp(os.path.getmtime(LogFile)).strftime('%Y-%m-%d %H-%M-%S')
      copyfile(LogFile, 'Log/'+LogFile.replace('.txt',' '+lastmod+'.txt'))
      os.remove(LogFile)
  </code></pre>
  <p>The full error message is:</p>
  <pre><code>---------------------------------------------------------------------------
  PermissionError                           Traceback (most recent call last)
  &lt;ipython-input-43-cb0f2cf501af&gt; in &lt;module&gt;
        8     lastmod = datetime.datetime.fromtimestamp(os.path.getmtime(LogFile)).strftime('%Y-%m-%d %H-%M-%S')
        9     copyfile(LogFile, 'Log/'+LogFile.replace('.txt',' '+lastmod+'.txt'))
  ---&gt; 10     os.remove(LogFile)
  
  PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'CCH Prepare Delta.txt'
  </code></pre>
  <p>As I know, after using copyfile, the file is closed by itself? Previously I also had the same trouble with os.rename. I found a manual method which is end process in Microsoft Resource Monitor but it seems not dynamic. Wanna find a solution right in the code itself.</p>
  <p>Much appreciate your help in advance!</p>