Avoiding "WindowsError: [Error 5] Access Denied"

There is a script to re-create the folder:

# Remove folder (if exists) with all files if os.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") 

This works almost always, but in some cases (at the creation step) I get

 WindowsError: [Error 5] Access is denied: 'C:\\Path\\To\\my_folder' 

What can cause this error and how to avoid it?

+5
source share
5 answers

What can cause this error?

You simply do not have access to the folder in which you are writing for the current process (python.exe) or, possibly, even for the user. If your user is not an administrator, there may be directories for which you do not have write permissions.


How can i avoid this?

In general, to avoid such an exception, you can use the try and except block, in which case it will be an IOError . Therefore, if you just want to ignore access denied and continued with a script, you can try:

 try: # Remove folder (if exists) with all files if os.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 really did not expect this error, and this should not happen, you need to change the permissions for the file. Depending on your user rights, you can take various steps.

  • User who can run programs as Admin: Option A

    • Right click on cmd.exe .
    • Click "Run as administrator."
    • Go to your script location via cd , as it will open in C:\Windows\system32 if you do not edit certain parameters.
    • Run script > python myscript.py .
  • User who can run programs as Admin: Option B

    • Open file explorer.
    • Browse to the folder or folders that you want to record.
    • Right click on it.
    • Select "Properties."
    • In the properties window, select the security tab.
    • Click "Edit" and edit it as you wish, or you need to provide access to programs or users.
  • User without administrator privileges:

    • This probably means that it is not your computer.
    • Check the PC help desk if you work in Uni or Work, or ask your teacher if at school.
    • If you are at home and your computer means that you are logged in with a user who is not an administrator. By default, you create the first one. If yes, check the user settings on the control panel.
    • From there, the rest is almost the same afterwards.
+6
source

See the RemoveDirectory documentation; "The RemoveDirectory function marks the directory for deletion at close. Therefore, the directory is not deleted until the last directory descriptor is closed."

This means that if something manages to create a descriptor in the directory you are deleting (between creation and deletion), the directory will not actually be deleted, and you will get your "Access is denied",

To resolve this problem, rename the directory you want to delete before deleting it.

So,

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

may fail, resolve with

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

Permissions may be a problem, but I had the same problem: "[Error 5] Access is denied" on os.rename() , and a simple retry loop could rename the file after several attempts.

 for retry in range(100): try: os.rename(src_name,dest_name) break except: print "rename failed, retrying..." 
+4
source

This is because you are not checking to see if you have permission to open this path. You need to change permissions for these folders.

+1
source

Create a python script file. In this case, you can copy it to C: \ WINDOWS \ system32. The script file creates a folder named "Smaog"

 import os os.chdir('C:/Program Files') os.makedirs('Smaog') 

Create a batch file in any folder that you like.

 echo off title Renaming Folder python sample.py pause 

Save the batch file. To start it, right-click and select "Run as administrator"

Although you can do this instead if you don't want to put your python script in C: \ WINDOWS \ system32. In your batch file, specify the folder / directory where your python script file is located.

 echo off title Renaming Folder cd c:\Users\Smaog\Desktop python sample.py pause 

Then run it as an Administrator, as described above.

+1
source

All Articles