The Python Oracle

Deleting folders in python recursively

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping

--

Chapters
00:00 Question
01:12 Accepted answer (Score 654)
01:25 Answer 2 (Score 42)
01:43 Answer 3 (Score 37)
02:02 Answer 4 (Score 15)
02:15 Thank you

--

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

Accepted answer links:
[shutil.rmtree]: https://docs.python.org/library/shutil.h...

Answer 2 links:
[pathlib]: https://docs.python.org/library/pathlib....

Answer 3 links:
[os.walk()]: https://docs.python.org/3/library/os.htm...

Answer 4 links:
[shutil]: https://docs.python.org/library/shutil.h...

--

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')