0

I am trying to delete files from my download folder but I get and error that read PermissionError: [WinError 5] Access is denied: 'C:\\Users\\Downloads'

I tried running Visual Studio as admin and adding a code to elevate privileges but I still get the error

my code is

ASADMIN = 'asadmin'

if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
    shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)

def deleteFiles():
    folder = "C:\\Users\\Downloads"
    for f in glob.glob(folder):
        os.remove(f)
    return;

deleteFiles()

can anyone help with getting these files deleted? Thanks

jordan23
  • 73
  • 1
  • 12
  • You as a user do not have rights to that folder – mad_ Sep 07 '18 at 19:03
  • 1
    Possible duplicate of [How to avoid "WindowsError: \[Error 5\] Access is denied"](https://stackoverflow.com/questions/37830326/how-to-avoid-windowserror-error-5-access-is-denied) – mad_ Sep 07 '18 at 19:04
  • 4
    `glob.glob('C:\\Users\\Downloads')` does NOT return a list of files in that directory; it returns the directory name itself. Perhaps you meant `glob.glob('C:\\Users\\Downloads\\*.*')` instead? – John Gordon Sep 07 '18 at 19:04

1 Answers1

3

glob.glob() returns a list of all filenames matching a wildcard expression. i.e. if you passed it '/tmp/*.py', it might return the list ['/tmp/bar.py', '/tmp/baz.py', 'tmp/foo.py'].

You passed it a string which contains no wildcard characters, so it just returned the original string back to you, so your code ended up calling os.remove('C:\\Users\\Downloads').

John Gordon
  • 29,573
  • 7
  • 33
  • 58