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.
source share