-1

I want to delete a folder inside the Windows drive 'C'

but i get PermissionError

I used the 'os' module as usual but I do not know exactly how to solve the access problem

Code:

import os
os.remove("C:\\Users\name\AppData\Local\Temp")

Error:

line 2, in <module>
   os.remove("C:\\Users\name\AppData\Local\Temp")
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\name\\AppData\\Local\\Temp'
  • I think ```AppData``` is protected by windows. It is always ```hidden``` until you manually reveal it –  Aug 06 '21 at 15:52
  • Did you check this: https://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder – Sup Aug 06 '21 at 15:52
  • 1
    Is the real path **exactly** as posted here? Cause you have: *"C:\\Users **\n** ame\AppData\Local\Temp"*. Double the backslashes (although in the exception text they are *OK*). What does *os.path.isdir* indicate for that folder? **Is the user *name* same user that runs the script**? – CristiFati Aug 06 '21 at 16:07
  • So, after all, it is a *dupe* of https://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder. Unfortunately, I can't mark the question again. – CristiFati Aug 06 '21 at 18:56

2 Answers2

2

In order to delete a folder in Python you can use os.rmdir but only for empty directories

For non-empty ones you can use shutil.rmtree, see https://docs.python.org/3/library/shutil.html#shutil.rmtree

Peter Burkert
  • 461
  • 4
  • 13
2

Use this:

import shutil
shutil.rmtree(folder_name)

This deletes the folder, even if it contains files.

Despereaux
  • 201
  • 2
  • 10