Deleting Directories

L

Laura McCord

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?

Here is the code:

dir = "/var/www/html/da"
currentTime = int(time.time())
print currentTime
dirfiles = os.listdir(dir)
print dirfiles
for name in dirfiles:
dirpath = os.path.join(dir, name)
mod_time = os.path.getmtime(dirpath)
timeDiff = currentTime - mod_time
if timeDiff > maxOld:
print dirpath

Thanks,
Laura
 
R

Rico Huijbers

Laura said:
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?

Assuming you already figured out how to check the directories'
timestamps, shutil.rmtree() will do what you want.
 
P

Peter Hansen

Laura said:
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
 
P

Peter Hansen

Peter said:
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
 
3

336699

Look at the docs for the shutil module. It has a function called
rmtree() that will do what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top