Home > OS >  Deleting folders using os.remove() in Python
Deleting folders using os.remove() in Python

Time:10-03

I want to delete multiple folders mentioned in list N using os.remove() but I see an error. How can I fix it?

import os

N=[1,3]

for i in N:
    os.remove(rf"C:\Users\User\Test1\{i}")

The error is

PermissionError: [WinError 5] Access is denied: 'C:\\Users\\User\\Test1\\1'

CodePudding user response:

You could try to do with shutil module:

import shutil
N=[1,3]

for i in N:
    shutil.rmtree(rf"C:\Users\User\Test1\{i}")
  • Related