'WinError 5 Access is denied: 'C:\\Program Files (x86)

I am trying to delete a temp file which a specific game uses with Python. I am running CMD as admin and using code:

path = ""
if os.path.exists(path):
    os.remove(path)

This is giving me the error access denied likely because it is program files but any way around this?



Solution 1:[1]

Based on comments added to the question, it was misleading - you weren't trying to remove a file from Program Files, you were trying to remove a directory from AppData\Local. There shouldn't be any problem with that, except that you're using os.remove to do it. According to the documentation:

If path is a directory, an IsADirectoryError is raised. Use rmdir() to remove directories.

So the fix is simple:

os.rmdir(path)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Ransom