Coding⏱️ 3 min read📅 2026-06-19

How to Fix Error 32 Error – How to delete a folder in Python when [Error 32] is present

Error 32 occurs when a process is being used by another process, preventing deletion of a folder in Python.

Quick Answer: Use the `win32process` module to terminate the process holding the file, then attempt to delete the folder.

Error 32 occurs when a process cannot access a file because it is being used by another process. This issue affects users who are trying to delete a folder that is currently being monitored by a thread.

This error can be frustrating, especially when trying to clean up temporary files or directories. However, there are methods to resolve this issue and safely delete the folder.

🔍 Why This Happens

  • The primary cause of Error 32 is that the process attempting to access the file is still holding onto it, preventing other processes from accessing it.
  • An alternative reason for Error 32 could be due to a permissions issue or a system resource conflict.

🛠️ Step-by-Step Verified Fixes

Using the `shutil` module with the `try`-`except` block

  1. Step 1: Try deleting the folder using `shutil.rmtree(Location)` and catch the `OSError` exception that is raised when Error 32 occurs.
  2. Step 2: Use a try-except block to handle the error and attempt to delete the folder again after the process has released its hold on it.
  3. Step 3: If the error persists, consider using alternative methods such as `os.rmdir()` or `win32file.DeleteFile()` with the `bDelete` parameter set to `True`.

Using the `win32file` module

  1. Step 1: Use the `win32file.CreateFile()` function to create a handle to the folder being monitored.
  2. Step 2: Set the `FILE_LIST_DIRECTORY` flag and use the `bDelete` parameter to indicate that you want to delete the folder.
  3. Step 3: Wait for any pending operations to complete using the `WaitForSingleObject()` function before attempting to delete the folder.

🎯 Final Words

To safely delete a folder being monitored by a thread, try using the `shutil` module with a `try`-`except` block or the `win32file` module. If these methods fail due to Error 32, consider alternative approaches such as using `os.rmdir()` or `win32file.DeleteFile()`.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions