473,569 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting Directories


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(di r, name)
mod_time = os.path.getmtim e(dirpath)
timeDiff = currentTime - mod_time
if timeDiff > maxOld:
print dirpath

Thanks,
Laura

Jul 18 '05 #1
4 8856
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
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
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
Look at the docs for the shutil module. It has a function called
rmtree() that will do what you want.

"Laura McCord" <La**********@d oucet-austin.com> wrote in message news:<ma******* *************** *************** @python.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1772
by: phpfrizzle | last post by:
Hi there, I have built a CMS, and in one part you have to be able to delete images. Structure like this: _root --> folder "img" --> file "1.jpg" --> file "2.jpg" --> folder "CMS" --> file "edit_text.php" --> file "delete_img.php" (function 'unlink()' in it)
1
582
by: Laura McCord | last post by:
Hi, I need to delete all directories in /var/www/html/test that are older than five days. I am new to python so how would I get started? Any tips or suggestions would be appreciated. Thanks, Laura
4
1811
by: Guinness Mann | last post by:
Greetings, I can't figure out what to search on to find the documentation on how to completely delete a "solution" from VS.NET 2003. I have no problem at all deleting projects, but even if I delete the entire underlying directory structure, the solutions won't go away. Anybody know how to do that? -- Rick
6
506
by: Mark | last post by:
I get an error message when deleting rows from a table in Access database. My understanding is that the error message relates to the sharing of the Inetpub and the wwwroot directory. While I have set the directories to "Share" in the properties window, it still does not allow row removal. I'm running Microsoft Windows XP, Professional,...
5
1466
by: Casanova http://www.prashanthmohan.tk | last post by:
Hello!! How can i delete files and folders using file hadnling in C++? Thank You
6
4548
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application restarts and all active sessions are terminated. This error is also described in detail here:...
4
3202
by: clintonG | last post by:
I started an application using Beta2 and now I've been having a variety of problems continuing to develop the application in 2.0 that I suspect may be related to left-over icky stuff. What can I delete under the Temporary ASP.NET Files directory? I assume everything under that directory ccan be whacked and if neccessary will be rebuilt by...
2
2532
by: Josh Kandiko | last post by:
Hi, I've been having some frustrations with deleting files from the Isolated Storage directories. Basically, I want my application to remove all instances of configuration information for my application when the application is uninstalled. So, I created an uninstaller class that overrides the uninstall method, as follows: private void...
1
1409
by: Lee | last post by:
Hi, when deleting a directory using;- string dir = "whatever"; Directory.Delete(dir); it fails if files/directories exist within the directory being deleted. is there an easy way of deleting the directory and all contents without having to recurse through the entire directory deleting each one ?
9
8342
by: potholer | last post by:
I have been asked to create a self deleting exe, it has been a curious task that has proved fruitless I have searched in various places but to no avail I can do it in vb6 using a batch file, but I have been advised it would be better to use C or C++ does anyone know how to do this and the call it from vb6? thanks in advance
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.