[Tutor] checking and deleting oldest
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun Dec 15 18:28:02 2002
On Sun, 15 Dec 2002, Kyle Babich wrote:
> I'm trying to count the number of folders in a specific file and if that
> number is above a certain amount then the oldest file is deleted.
Hi Kyle,
I think you meant to say this instead:
"""I'm trying to count the number of files in a specific folder and if
that number is above a certain amount then the oldest file is deleted."""
> The first part is easy but how would I check the dates and delete the
> oldest file?
One approach could be to take a list of those files, and then sort them by
date. Once we have a sorted list of files, it's fairly easy to pick out
either the newest or oldest file, since they'll be at the edges of our
list.
People on Tutor talked a little about sorting a week ago; you might find
this thread useful:
http://mail.python.org/pipermail/tutor/2002-December/019252.html
http://mail.python.org/pipermail/tutor/2002-December/019219.html
as well as the Sorting HOWTO:
http://www.amk.ca/python/howto/sorting/sorting.html
The big trick is to write a comparison function that tells sort() how
to order files by date. But for that, there's a function in
os.path() called "getmtime()", which tells us when a file was last
modified.
http://www.python.org/doc/lib/module-os.path.html
Try working it out; if you run into problems, please feel free to ask more
questions. Good luck to you!