What this error actually says
Error 5 in Python file security means Windows denied permission to change a file's security settings. When Python tries to alter who can read, write, or execute a file — usually through commands like os.chmod() or when a program installs itself — Windows blocks it and returns error code 5, which translates to "Access Denied."
This is not a bug in your code. It is Windows enforcing a rule: either you do not have permission to change that file's settings, or the file is locked by another program that will not let go of it.
The error appears most often when you are running Python without administrator rights, when a file is open in another program, or when you are trying to change permissions on a system file that Windows protects.
Key Takeaways
- Error 5 means Windows blocked your attempt to change file permissions, not that your code is wrong.
- The three most common causes are: running Python without administrator rights, another program holding the file open, or trying to modify a protected system file.
- Right-clicking the Python script or command prompt and selecting "Run as administrator" fixes the problem in most cases.
- If a file is locked by another program, close that program first, then try again.
- Some files (Windows system files, files in Program Files) cannot have their permissions changed by regular users, even with administrator rights.
Why Windows blocks permission changes
Windows treats file permissions as a security boundary. If any user could change any file's permissions at any time, someone could lock you out of your own files or expose system files to tampering. So Windows enforces a rule: you can only change permissions on files you own, and only if you have the authority to do so.
When Python runs without administrator rights, it inherits that limitation. Even if you own the file, Python cannot change its security settings because the user account running Python does not have the power to do so. Error 5 is Windows saying "I believe you, but I cannot let this happen from this account."
The three most common causes
Running Python without administrator rights. If you open Command Prompt or PowerShell normally and run a Python script that tries to change file permissions, Windows blocks it. The fix is to right-click Command Prompt or PowerShell, select "Run as administrator," and run your script again.
Another program has the file open. If a file is currently open in Notepad, Visual Studio Code, an antivirus scanner, or any other program, Windows locks it. Python cannot change the permissions of a locked file. Close the program holding the file, then run your Python script again.
The file is a system file or in a protected folder. Files in C:\Windows, C:\Program Files, or C:\Program Files (x86) are protected by Windows, even if you run Python as administrator. You cannot change their permissions from a regular user account. If your script needs to modify files in these locations, it needs to run as a service or system account, which is beyond what a normal Python script can do.
How to fix it: step by step
Step 1: Run Python as administrator. Right-click Command Prompt, PowerShell, or your Python IDE and select "Run as administrator." A window will appear asking "Do you want to allow this app to make changes to your device?" Click "Yes." Then run your script again.
Step 2: Close any program using the file. If you are trying to change permissions on a file, make sure it is not open in any other program. Check your taskbar and system tray for text editors, IDEs, antivirus software, or file explorers that might have the file locked. Close them and try again.
Step 3: Check the file location. If the file is in C:\Windows, C:\Program Files, or another system folder, you cannot change its permissions from Python. Move the file to a folder you control, like your Documents or Desktop folder, and try again.
Step 4: Restart your computer. If a file is still locked after you close the program using it, a restart will release the lock. This is rare but happens when a program crashes or does not fully release the file.
What your code should do instead
Rather than trying to change file permissions after the fact, create files with the correct permissions from the start. In Python, you can use the os.open() function with a mode parameter that sets permissions when the file is created, rather than changing them afterward.
If you are writing a program that needs to modify file permissions regularly, consider whether you actually need to. Most Python programs work fine without touching file permissions at all. If you do need to, document that your program must run as administrator and explain why in your README or installation instructions.
When this error appears in installers
If you see this error while installing a Python package or process, the installer is trying to set file permissions and does not have the rights to do so. The fix is the same: run the installer as administrator. Right-click the installer file, select "Run as administrator," and proceed.
If the installer still fails, check whether another program is using files in the installation folder. Antivirus software sometimes locks files during installation. Temporarily disable your antivirus, run the installer again, then turn it back on.
Frequently Asked Questions
Does error 5 mean my file is corrupted?
No. Error 5 is a permission issue, not a file corruption issue. Your file is fine; Windows just will not let Python change its security settings. The file itself is readable and usable.
Why does my script work on my friend's computer but not mine?
Your friend probably runs Python as administrator, or the file is in a location where your user account has more permissions. Ask them whether they right-click to run as administrator, or whether the file is in a different folder.
Can I fix this by changing my Windows user account type?
Changing your account to administrator will help, but it is not the best solution. Instead, run Python as administrator when you need to change file permissions. This keeps your day-to-day account safer while still letting you do what you need to do.
What if I need to change permissions on files in Program Files?
You cannot do this from a regular Python script, even as administrator. Those files are protected by Windows. If your program needs to modify files there, it needs to run as a Windows service, which requires different setup. For most cases, store your files in a user folder like Documents instead.
Does this error happen on Mac or Linux?
No. Mac and Linux use different permission systems. On those systems, you would see a different error message. Error 5 is Windows-specific.