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'
Answers:
You could try to do with shutil
module:
import shutil
N=[1,3]
for i in N:
shutil.rmtree(rf"C:UsersUserTest1{i}")
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'
You could try to do with shutil
module:
import shutil
N=[1,3]
for i in N:
shutil.rmtree(rf"C:UsersUserTest1{i}")