[Tutor] Help listing directory timestamps and deleting directories
Karim
karim.liateni at free.fr
Mon Jan 24 23:30:57 CET 2011
Hello,
Also working w/o external module just the standard one:
*>>> import datetime
>>> remove_after = datetime.datetime.now() - datetime.timedelta(days=31)
>>> remove_after
datetime.datetime(2010, 12, 24, 23, 21, 10, 11315)
*
Regards
Karim
On 01/24/2011 08:02 PM, Vince Spicer wrote:
>
>
> On Mon, Jan 24, 2011 at 11:25 AM, bsdwiz at gmail.com
> <mailto:bsdwiz at gmail.com> <bsdwiz at gmail.com <mailto:bsdwiz at gmail.com>>
> wrote:
>
> Hi, hoping for some help here. I've been trying to write a python
> script (complete newb) and have spent several days trying to get
> this right with no success.
>
> I am trying to list timestamps in a directory and if they are
> older than x amount of days delete the directories. It seems that
> in my for loop it is only evaluating the last timestamp of the
> last directory and using that timestamp to make the deletion
> decision. I realize that this script isn't going to delete the
> directories with evaluate as true but I haven't been able to get
> that far yet...
>
>
>
> ###########start script##############
> ###Modules###
>
> import os
> import time
>
> ###Global Variables###
>
> curr_t = time.time()
>
> days = 2629743 #2629743 is 30 days in epoch time.
>
> directory=os.path.join("/home", "userid", "python")
>
>
> for r,d,f in os.walk(directory):
> for dir in d:
> timestamp = os.path.getmtime(os.path.join(r,dir)) ## It
> seems that the below "if" statement is only comparing the
> timestamp of the last directory that this variable lists.
> print timestamp
> if curr_t - days < timestamp: #### I am
> expecting this statement to compare each timestamp of the
> "directory" variable and print out if it should be deleted or not.
> print "Your file will be deleted"
>
> else:
> print "File will NOT be deleted..."
>
>
> #######################end of script#############################
>
>
>
> Thank you for your time!
>
> _______________________________________________
> Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
> First off, I would recommend using dates and not times, this can help
> make things clearer
> when dealing with dates, also the dateutil module can make date math a
> lot simpler (easy_install dateutil)
>
>
> from datetime import datetime
> from dateutil.relativedelta import relativedelta
>
> remove_after = datetime.now() - relativedelta(days=31) # exactly 1
> month prior to today
> check_dir = "/path/to/whatever"
>
> for r,d,f in os.walk(check_dir):
> for dir in d:
> loc = os.path.join(r,dir)
> last_modified = datetime.fromtimestamp(os.path.getmtime(loc))
> if last_modifed < remove_after:
> print "delete old directory", loc
> else:
> print "keep recently modified", loc
>
>
>
> Hope this helps,
>
> Vince Spicer
> Developer
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110124/1767ad41/attachment.html>
More information about the Tutor
mailing list