Why do I have PermissionError: [WinError 5] in Python?

65 views Asked by At

Whenever I run my script, I get PermissionError: [WinError 5] access denied, unless I delete the target folder tuner_dir, and re-run the code. Here some of the code:

# Check if the tuner directory exists and is empty
if os.path.exists(tuner_dir) and not os.listdir(tuner_dir):
    print(f"The tuner directory '{tuner_dir}' is empty.")
else:
    print(f"The tuner directory '{tuner_dir}' is not empty or doesn't exist.")
    # Check the reset_tuner flag and optionally delete the contents of the directory
    if reset_tuner:
        for item in os.listdir(tuner_dir):
            item_path = os.path.join(tuner_dir, item)
            if os.path.isfile(item_path):
                os.remove(item_path)
                print(f"Removed file: {item_path}")
            elif os.path.isdir(item_path):  # Change to 'os.path.isdir'
                shutil.rmtree(item_path)
                print(f"Removed directory: {item_path}")

#The code continues on.

These are the only lines in the code that rever to this problematic tuner_dir. I have checked user rights for groups and users, I have a single user in my PC (only me), I run Python as Administrator by default (after looking for ways to solve this problem), and in Properties > Security I see Authenticated Users, System, Administrators and my own user with full rights in this working directory. I don't know what else to do. Everytime I need to manually delete the folder, and re-run the code, and while this is a workaround.

This is the resulting icacls over the target directory:

NT AUTHORITY\Authenticated Users:(I)(OI)(CI)(F)
BUILTIN\Users:(I)(OI)(CI)(F)
BUILTIN\Administrators:(I)(F)
BUILTIN\Administrators:(I)(OI)(CI)(IO)(F)
NT AUTHORITY\SYSTEM:(I)(F)
NT AUTHORITY\SYSTEM:(I)(OI)(CI)(IO)(F)
NT AUTHORITY\Authenticated Users:(I)(OI)(CI)(IO)(M)
BUILTIN\Users:(I)(OI)(CI)(IO)(GR,GE)
0

There are 0 answers