Is there a library equivalent of "rm -rf" ?

Andrew Koenig ark at
Wed Oct 30 23:59:23 CET 2002

I couldn't find a library function to (recursifely) remove a directory
and everything in it.

os.rmdir removes a directory, but only if it's empty.

os.removedirs removes a file, then pruns empty directories above it.

Did I miss something?

The obvious question is:  ``Why do I care?  It's trivial to write.''
The answer is that it's not quite trivial in the presence of symbolic
links: If the function encounters a symbolic link, it should not
follow it even if it refers to a directory.  So the obvious code,
which checks for a directory and traverses it if it gets one,
is wrong--and dangerously so.

Andrew Koenig, ark at,

More information about the Python-list mailing list