Skip to content Skip to sidebar Skip to footer

How To Avoid "windowserror: [error 5] Access Is Denied"

There's the script to re-create folder: # Remove folder (if exists) with all files if os.path.isdir(str(os.path.realpath('..') + '\\my_folder')): shutil.rmtree(os.path.real

Solution 1:

See RemoveDirectory documentation; "The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed."

This means that if something manages to create a handle to the directory you remove (between creation and removal) then the directory isn't actually removed and you get your 'Access Denied',

To solve this rename the directory you want to remove before removing it.

So

while True:
  mkdir('folder 1')
  rmdir('folder 1')

can fail, solve with;

while True:
  mkdir('folder 1')
  new_name = str(uuid4())
  rename('folder 1', new_name)
  rmdir(new_name)

Solution 2:

Permissions might be the problem, but I had the same problem '[Error 5] Access is denied' on a os.rename() and a simple retry-loop was able to rename the file after a few retries.

for retry inrange(100):
    try:
        os.rename(src_name,dest_name)
        breakexcept:
        print"rename failed, retrying..."

Solution 3:

What could cause this error?

You simply do not have access to the folder you are writing in for the process that is currently running (python.exe), or maybe even for the user. Unless your user is an admin there may be directories for which you do not have write permissions.


How can I avoid it?

In general to avoid such an exception, one would use a try and except block, in this case it would be an IOError. Therefore if you just want to overlook access denied and continue with the script you can try:

try:
    # Remove folder (if exists) with all files
    ifos.path.isdir(str(os.path.realpath('..') + "\\my_folder")):
        shutil.rmtree(os.path.realpath('..') + "\\my_folder", ignore_errors=True)
    # Create new folder
    os.mkdir(os.path.realpath('..') + "\\my_folder")
except IOError:
    print("Error upon either deleting or creating the directory or files.")
else:
    print("Actions if file access was succesfull")
finally:
    print("This will be executed even if an exception of IOError was encountered")

If you truly were not expecting this error and it is not supposed to happen you have to change the permissions for the file. Depending on your user permissions there are various steps that you could take.

  • User that can execute programs as Admin:Option A

    1. Right-Click on cmd.exe.
    2. Click on Run as Administrator.
    3. Go to your script location via cd since it will be opened at C:\Windows\system32 unless you have edit certain parameters.
    4. Run your script > python myscript.py.
  • User that can execute programs as Admin:Option B

    1. Open file explorer.
    2. Go to the folder, or folders, you wish to write in.
    3. Right-Click on it.
    4. Select Properties.
    5. In the properties window select the security tab.
    6. Click Edit and edit it as you wish or need to give access to programs or users.
  • User with no Admin privileges:

    1. This probably means it is not your computer.
    2. Check for the PC help desk if at Uni or Work or ask your teacher if at School.
    3. If you are at home and it is your computer that means you have logged in with a non-admin user. The first one you create typically is by default. Check the user settings in the Control Panel if so.
    4. From there on the rest is pretty much the same afterwards.

Solution 4:

For me it worked in this way:

whileos.path.isdir (your_path):
    shutil.rmtree (your_path, ignore_errors=True)
os.makedirs (your_path)

Solution 5:

It happens because you are not checking if you have permissions to open that path. You need to change the permissions on those folders.

Post a Comment for "How To Avoid "windowserror: [error 5] Access Is Denied""