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\")):
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 in range(100):
try:
os.rename(src_name,dest_name)
break
except:
print "rename failed, retrying..."
For me it worked in this way:
while os.path.isdir (your_path):
shutil.rmtree (your_path, ignore_errors=True)
os.makedirs (your_path)
It happens because you are not checking if you have permissions to open that path. You need to change the permissions on those folders.
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
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 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
cmd.exe
.cd
since it will be opened at C:\Windows\system32
unless you have edit certain parameters.> python myscript.py
.User that can execute programs as Admin: Option B
User with no Admin privileges:
I had this problem last night after switching Py2 to Py3 and realized that I was installing it for all users. That means you are installg it into Program Files directory not instead of the %AppData%. Mostly running as administrator solves the problem as some of you said above but I use VSCode and sometimes PyCharm and love to use the terminal in them. Even if you try to run these programs as an administator you have lots of annoying times when trying to focus on your lovely code.
My Solution :
1 ) Full uninstall (including Py Launcher)
2 ) Then install with custom install with the provided installer BUT ...
3 ) DO NOT choose the INSTALL FOR ALL USERS option.
I think that will make your day much more easy without any "[Error 5]" lines at your command prompt as it worked for me.
Create your python script file. In this case you may copy it in C:\WINDOWS\system32. The script file is creating a folder named "Smaog"
import os
os.chdir('C:/Program Files')
os.makedirs('Smaog')
Create batch file, at any folder you like.
echo off
title Renaming Folder
python sample.py
pause
Save the batch file. To run it, right click and choose Run as Administrator
Though you may choose to do this instead if you don't want to put your python script in C:\WINDOWS\system32. In your batch file, indicate folder/directory where your python script file resides.
echo off
title Renaming Folder
cd c:\Users\Smaog\Desktop
python sample.py
pause
Then run it as Administrator as explained just above.