[Tutor] Help listing directory timestamps and deleting directories

bsdwiz at gmail.com bsdwiz at gmail.com
Mon Jan 24 18:25:53 CET 2011


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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110124/bce6459e/attachment.html>


More information about the Tutor mailing list