Deleting folders using os.remove() in Python

Question:

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:UsersUserTest1{i}")

The error is

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

||

Answers:

You could try to do with shutil module:

import shutil
N=[1,3]

for i in N:
    shutil.rmtree(rf"C:UsersUserTest1{i}")
Answered By: ramsay
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.