When using python os.rmdir, get PermissionError: [WinError 5] Access is denied

uncheck read-only attribute box found in the properties of the file/folder. enter image description here


Try deleting all files in the directory before deleting the directory:

import os
path_to_dir  = 'C:\\Users\\Desktop\\temp'  # path to directory you wish to remove
files_in_dir = os.listdir(path_to_dir)     # get list of files in the directory

for file in files_in_dir:                  # loop to delete each file in folder
    os.remove(f'{path_to_dir}/{file}')     # delete file

os.rmdir(path_to_dir)                      # delete folder

I had a same issue, could do it via shutil module.

import shutil
shutil.rmtree('/path/to/your/dir/')

I found a solution here: What user do python scripts run as in windows?

It seems as if the offending folder has a stubborn read-only attribute. Adding in a handler to change such read-only flags worked like a charm for me.

All of you who posted suggestions, you helped me track down the final answer, so thank you!