Remove directory tree without following symlinks

Steven D'Aprano steve at pearwood.info
Fri Apr 22 10:56:33 EDT 2016


I want to remove a directory, including all files and subdirectories under
it, but without following symlinks. I want the symlinks to be deleted, not
the files pointed to by those symlinks.

E.g. if I have this tree:


parent/
+-- spam/
:   +-- a.txt
:   +-- b.txt
:   +-- eggs/
:   :   +-- c.txt
:   :   +-- surprise -> ../../parent
:   +-- d.txt
+-- e.txt

and I call remove_tree("parent/spam"), I want the result to be:

parent/
+-- e.txt


(Assuming that I have permission to delete all the files and directories.)

What should I use for "remove_tree"? Do I have to write my own, or does a
solution already exist?




-- 
Steven




More information about the Python-list mailing list