PermissionError: [WinError 32] means Windows refused to delete, rename or open a file because some process still has it open, and very often that process is your own script. First check your code for a file handle that is still open, then, if the code is clean, find the outside process holding the file. LockGhost from Automata Labs shows exactly which app or service has the file open, with a friendly name instead of a bare process ID, so you know whether to close an editor, wait for antivirus or fix your own code.
See which process is holding the file before you touch your code.
Get it fromMicrosoft
What does WinError 32 actually mean?
The number comes from Windows, not Python. Microsoft’s system error codes define ERROR_SHARING_VIOLATION as 32 (0x20). It means an open handle’s sharing mode conflicts with the operation, even if that handle belongs to your own process. Python’s OSError documentation explains that winerror gives the native Windows error code, while errno is an approximate POSIX translation. Python reports this sharing violation as PermissionError.
It is a Windows behavior. Python’s documentation for os.remove() says that on Windows, attempting to remove a file that is in use raises an exception, while on Unix the directory entry is removed even if the file is still open. That is why a script that works on Linux or macOS can fail on Windows with WinError 32.
| Code | Windows name | Meaning |
|---|---|---|
| 32 | ERROR_SHARING_VIOLATION | The file is open in another process, or another handle in yours |
| 33 | ERROR_LOCK_VIOLATION | Another process has locked a portion of the file |
| 5 | ERROR_ACCESS_DENIED | Usually permissions or a read-only file, not a lock |
If you see 5 instead of 32, look at permissions and read-only attributes before hunting for locks.
Is my own Python script holding the file?
Check this first because it is a cause you can verify directly. Typical patterns:
A file opened without a context manager.
import os
f = open("report.csv")
data = f.read()
os.remove("report.csv") # WinError 32: f is still open
Use with so the handle closes before you delete or rename:
import os
with open("report.csv") as f:
data = f.read()
os.remove("report.csv")
A library that keeps the file open. Image, spreadsheet, PDF and logging libraries can hold a handle until you close the object. Close it explicitly, or use its own context manager, before moving or deleting the file. For logging, close and remove the FileHandler before you rotate or delete the log file yourself.
NamedTemporaryFile reopened by name. Python’s tempfile documentation says that on Windows, reopening a named temporary file by name while it is still open only works if at least one condition is met: delete is false, the additional open shares delete access, or delete is true but delete_on_close is false. In the last case, any extra opens must be closed before the context manager exits, or the final delete fails with a PermissionError. The delete_on_close parameter was added in Python 3.12, so on older versions use delete=False and remove the file yourself when you are done.
import tempfile
with tempfile.NamedTemporaryFile(delete=True, delete_on_close=False) as tmp:
tmp.write(b"data")
tmp.close()
with open(tmp.name, "rb") as reader:
data = reader.read()
# The context removes the temporary file after the reader closes.
See Python’s NamedTemporaryFile documentation for the Windows sharing and cleanup conditions.
A subprocess still running. If you start a tool with subprocess.Popen and it writes to the file, wait for it to finish before touching the file.
How do I find which other process is using the file?
If your code closes everything and the error persists, something outside Python holds the file. Common holders on a developer machine:
- Your editor or IDE, especially with the file open in a tab or a preview.
- Excel, which locks a CSV or XLSX for as long as the workbook is open.
- Antivirus or indexing briefly scanning a newly written file.
- Sync clients such as OneDrive uploading the file.
- A second copy of your script, or a Jupyter kernel that opened the file earlier and never closed it.
LockGhost answers the question directly: point it at the file or folder, and it shows the process holding it, with safeguards that help you avoid ending a critical system process. Everything stays on your PC. For the general Windows version of this error outside Python, see file being used by another process (error 32).
Name the process behind the lock in seconds, then decide whether to close it or wait.
Get it fromMicrosoft
How do I make cleanup code survive a brief lock?
Some locks are short. Antivirus or a sync client may hold a new file for a moment. For those cases a short retry is reasonable:
import os, time
def remove_with_retry(path, attempts=5, delay=0.5):
for i in range(attempts):
try:
os.remove(path)
return
except PermissionError as e:
if getattr(e, "winerror", None) != 32 or i == attempts - 1:
raise
time.sleep(delay)
Check winerror so you only retry sharing violations, not real permission problems. Do not retry forever. A lock that lasts more than a few seconds is a process you need to identify.
For temporary folders, Python’s tempfile.TemporaryDirectory accepts ignore_cleanup_errors=True. The documentation says that with it, errors during cleanup, such as a PermissionError removing open files on Windows, are ignored and the remaining items are deleted on a best-effort basis. For whole trees, shutil.rmtree takes ignore_errors, or an error handler through onexc (older Python versions use onerror). Ignoring errors leaves files behind, so log what was skipped.
Which fix fits which situation?
| Situation | Fix |
|---|---|
| Your code opens the file, then deletes or renames it | Use with, or close the object first |
| Reopening a NamedTemporaryFile by name | Use delete_on_close=False and close before reopening |
| Brief lock right after writing | Retry a few times on winerror 32 |
| Temp folder cleanup fails in tests | ignore_cleanup_errors=True, then log leftovers |
| Lock persists, code is clean | Find the holder with LockGhost, close that app |
| Build or copy step fails with the same message | See Robocopy error 32 or MSB3027 |
| Message mentions a locked portion | That is error 33, see error 0x80070021 |
Frequently asked questions
Why does the same script work on Linux but fail on Windows?
Python’s documentation notes that on Unix, removing a file that is still open removes its directory entry, while on Windows it raises an exception. Close the file before removing it and the script works on both.
Why does it happen in Jupyter but not when I run the script?
A notebook kernel keeps running between cells. A file opened in an earlier cell, and never closed, stays open until you close it or restart the kernel. Restarting the kernel releases every handle it held, which is a quick way to confirm the notebook was the holder.
Why do my tests fail with WinError 32 only on Windows CI?
Tests often write a file, assert on it and delete it in teardown. If any fixture or library still has the file open at teardown, Windows refuses the delete. Close objects inside the test, use context managers in fixtures, and use ignore_cleanup_errors=True for temporary directories where leftovers are acceptable.
Is WinError 32 the same as Access is denied?
No. Access is denied is Windows error 5, usually permissions or read-only attributes. WinError 32 is a sharing violation, meaning the file is open somewhere.
Should I kill the process holding the file?
Only if you know what it is. Closing an editor tab or Excel workbook is safe. Ending a system or antivirus process is not. LockGhost shows friendly names so you can tell which is which.
Does LockGhost send my file paths anywhere?
No. It runs offline, and its lock history stays on your PC.
Try LockGhost
Get LockGhost from the Microsoft Store. It is a one-time purchase with no subscription, and the Store listing shows the current price.
Find what is holding a file, folder or log open, without guessing in Task Manager.
Get it fromMicrosoft
Python is a trademark of the Python Software Foundation. Microsoft, Windows, Excel and OneDrive are trademarks of the Microsoft group of companies. LockGhost is not affiliated with or endorsed by the Python Software Foundation or Microsoft.
Last updated 28 October 2026. Published by Automata Labs, a software company in Canada.