title: How to fix PermissionError: [WinError 32] with os.remove in python?
body: <p>There was a <strong>PermissionError:[WinError 32]</strong>  problem in my code. The file must be decrypted first, then deleted (encrypted). But the file is only decrypted and two files remain in the folder: encrypted and decrypted.</p>
  
  <p>What could be the problem?</p>
  
  <p>I already tried to delay between decryption and deletion.</p>
  
  <pre><code>def decrypt(file):
  
      file_in = open(file, "rb")
      file_out = open(str(file[:-4]), "wb")
      rivate_key = RSA.import_key(open("private.pem").read())
  
      enc_session_key, nonce, tag, ciphertext = \
         [ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]
  
      cipher_rsa = PKCS1_OAEP.new(private_key)
      session_key = cipher_rsa.decrypt(enc_session_key)
  
      cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
      data = cipher_aes.decrypt_and_verify(ciphertext, tag)
      file_out.write(data)
      print(file + " DECRYPT!")
      time.sleep(5)
      os.remove(file)
  </code></pre>
  
  <p><strong>The following errors go:</strong></p>
  
  <pre><code>&gt;Traceback (most recent call last):
  File "C:/Users/user/Desktop/codes on python/decrypt.py", line 31, in &lt;module&gt;
      walk("C:\\Users\\user\\Desktop\\codes on python\\papka")  
  File "C:/Users/user/Desktop/codes on python/decrypt.py", line 28, in walk
      if os.path.isfile(path): decrypt(path)  
  File "C:/Users/user/Desktop/codes on python/decrypt.py", line 23, in decrypt
      os.remove(file)
  PermissionError: [WinError 32] Процесс не может получить доступ к файлу, так как этот файл занят другим процессом: 'C:\\Users\\user\\Desktop\\codes on python\\papka\\data.data.bin'
  </code></pre>