Windows Error 5: Access Is Denied When Trying Delete A Directory In Windows
Solution 1:
This was due to the file permissions issue.
You need to have the permissions to perform that task on that file.
To get the permissions associated with a file, useos.stat(fileName)
You can explicitly check the write permission for that file using os.access(fileName, os.W_OK)
Then, to change the permission, os.chmod(fileName,permissionNumeric)
.
Ex: os.chmod(fileName, '0777')
To change the permission for the current file that is being executed,
use os.chmod(__file__, '0777')
Solution 2:
I use pydev. And my solution is:
- Stop Eclipse.
- Start Eclipse with option Run as administrator
Solution 3:
takeown /F C:\<dir> /R /A
icacls C:\<dir> /grant administrators:F /t
Give ownership to administrators and give full control to administrators, if your user is an administrator.
Solution 4:
in order to change files located in "C:" you must have admin privileges, you can either get them before starting the script or while doing so, for instance:
#!python# coding: utf-8import sys
import ctypes
defrun_as_admin(argv=None, debug=False):
shell32 = ctypes.windll.shell32
if argv isNoneand shell32.IsUserAnAdmin():
returnTrueif argv isNone:
argv = sys.argv
ifhasattr(sys, '_MEIPASS'):
# Support pyinstaller wrapped program.
arguments = map(unicode, argv[1:])
else:
arguments = map(unicode, argv)
argument_line = u' '.join(arguments)
executable = unicode(sys.executable)
if debug:
print'Command line: ', executable, argument_line
ret = shell32.ShellExecuteW(None, u"runas", executable, argument_line, None, 1)
ifint(ret) <= 32:
returnFalsereturnNoneif __name__ == '__main__':
ret = run_as_admin()
if ret isTrue:
print'I have admin privilege.'
raw_input('Press ENTER to exit.')
elif ret isNone:
print'I am elevating to admin privilege.'
raw_input('Press ENTER to exit.')
else:
print'Error(ret=%d): cannot elevate privilege.' % (ret, )
code taken from: How to run python script with elevated privilege on windows
script by: Gary Lee
Post a Comment for "Windows Error 5: Access Is Denied When Trying Delete A Directory In Windows"