PermissionError: [WinError 5] Access is denied PermissionError: [WinError 5] Access is denied python-3.x python-3.x

PermissionError: [WinError 5] Access is denied


I too had this problem, and after searching found a good solution.

Essentially, before calling os.remove(file_name) we need change file permissions.

  1. import stat
  2. Before calling os.remove, call os.chmod(file_name, stat.S_IWRITE)

For example:

import osimport statdef clean_thrash(path):    dirlist=get_dirlist(path)    for f in dirlist:        fullname=os.path.join(path,f)        if fullname == os.path.join(path,"thrash.txt"):            os.chmod(fullname , stat.S_IWRITE)            os.remove(fullname)        if os.path.isdir(fullname):            clean_thrash(fullname)

I hope this fixes your problem.


If you are using windows, you can simply do:

import shutilshutil.rmtree(directory_path)

Hope this works!


You have to be administrator user if you are on Windows or have to have sudo permissions if you are on Linux. try running code with sudo

see this answer https://stackoverflow.com/a/32199615/6356497