By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

Deleting Directories

P: n/a

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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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?


Assuming you already figured out how to check the directories'
timestamps, shutil.rmtree() will do what you want.
Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

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

"Laura McCord" <La**********@doucet-austin.com> wrote in message news:<ma*************************************@pyth on.org>...
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?

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.