Deleting Directories

Peter Hansen peter at engcorp.com
Fri May 21 11:23:15 EDT 2004


Laura McCord wrote:

> This is probably a very basic question but I started learning python. I
> am almost done writing my delete directory script but I am at a stand
> still right now. 
> I want to delete folders in my "/var/www/html/da" directory that are
> over 1 day old.
> 
> 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?

There is an example at the bottom of 
http://docs.python.org/lib/os-file-dir.html which does what you need.

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.

-Peter



More information about the Python-list mailing list