The Python Oracle

Deleting folders in python recursively

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Track title: CC E Schuberts Piano Sonata D 784 in A

--

Chapters
00:00 Deleting Folders In Python Recursively
01:06 Accepted Answer Score 715
01:19 Answer 2 Score 45
01:28 Answer 3 Score 37
01:45 Answer 4 Score 18
01:56 Answer 5 Score 8
02:10 Thank you

--

Full question
https://stackoverflow.com/questions/1311...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #directory

#avk47



ACCEPTED ANSWER

Score 723


Try shutil.rmtree:

import shutil
shutil.rmtree('/path/to/your/dir/')



ANSWER 2

Score 38


The default behavior of os.walk() is to walk from root to leaf. Set topdown=False in os.walk() to walk from leaf to root.




ANSWER 3

Score 18


Try rmtree() in shutil from the Python standard library




ANSWER 4

Score 8


better to use absolute path and import only the rmtree function from shutil import rmtree as this is a large package the above line will only import the required function.

from shutil import rmtree
rmtree('directory-absolute-path')