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, bsd...@gmail.com <mailto:bsd...@gmail.com> <bsd...@gmail.com <mailto:bsd...@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@python.org <mailto:Tutor@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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to