Deleting Directories

Peter Hansen peter at engcorp.com
Fri May 21 11:29:09 EDT 2004


Peter Hansen wrote:

>> But, when I find the folder a simple rmdir() command does not work
>> because the directory is not empty. What else do I need to do to delete
>> a directory that contains content?
> 
> I've also been able to get shutil.rmtree() to do it before, as I recall,
> with an appropriate error handler, though I vaguely recall it had to
> restart in some way after each directory which failed to be removed
> the first time because it was not empty.

Since Rico didn't give a disclaimer about the directories being
empty, I just did a little test and sure enough, rmtree will
wipe out everything without complaint.  As it turns out, the
only time you need special handling is if some of the files
are not deletable.

-Peter



More information about the Python-list mailing list