File deletion after 72 hours of creation

Tim Williams tim at tdw.net
Fri Mar 30 12:42:53 EDT 2007


On 29 Mar 2007 13:40:58 -0700, ALEXURC at gmail.com <ALEXURC at gmail.com> wrote:
> On Mar 29, 12:02 pm, s... at pobox.com wrote:
> >     Alex> I'm looking for a simple method to delete a folder after 72
> >     Alex> "Business hours" (saturday/sunday doesnt count) since its
> >     Alex> creation. Note that This is on a linux system and I realize that
> >     Alex> it will be the last modified time. These files wont be modified
> >     Alex> since their creation.
> >
> >     Alex> Im very confused on how to work with the number easily.
> >
> > Take a look at the dateutil module.  Its relativedelta object has some
> > weekday sense:
> >
> >    http://labix.org/python-dateutil
> >
> > Skip
>
> Hey, thanks for the reply, I was able to accomplish it (somewhat ugly)
> using the datetime module alone. Here is my code.
> the part with the timestamp.file is just a plaintext file containg
> "#year#month#day" that is created when my client side program copies
> files into my directory.
>
> Just posting the code so if anyone else is wondering how to do this
> ever maybe they can find some of this useful:
> ---
>
> #!/usr/bin/env python
>
> import datetime
> import os
> today = datetime.date.today().toordinal()
> serverFiles = os.listdir("/home/webserver/")
> theDirList=[]
> for xfile in serverFiles:
>         if os.path.isdir("/home/webserver/" + xfile) == True:
>                 theDirList.append(xfile)
>
>
> for xdir in theDirList:
>         foo=open("/home/webserver/"+ xdir + "/timestamp.file",'r')
>         parseMe = foo.readlines()
>         fileDate=parseMe[0].split("#")
>         fileYear = fileDate[1]
>         fileMonth = fileDate[2]
>         fileDay = fileDate[3]
>
>         age_of_file = today -
> datetime.date(int(fileYear),int(fileMonth),int(fileDay)).toordinal()
>         true_age = age_of_file
>         for i in range(age_of_file):
>                 d=
> datetime.date(int(fileYear),int(fileMonth),int(fileDay)+i)
>                 ourDay = d.weekday()
>                 if ourDay == 5 or ourDay == 6:
>                         true_age = true_age - 1
>
>  print xdir + " : " + str(true_age) + " days old"
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

You may have "over thought" the problem :)

(Not tested)
----------------------------------------------------------
import os, time

pth = '/home/webserver/'
a_day = 60*60*24
today = time.asctime().split()[0]
cutOffDate = time.time() - (3 * a_day)
if today in ['Mon','Tue','Wed']:
    cutOffDate = cutOffDate - (2 * a_day)

for f in os.listdir( pth ):
 	fname = os.path.join(pth ,f)
 	if os.path.isfile( fname ) and os.path.getmtime(fname) < cutOffDate:
 			print fname # os.remove(fname)

------------------------------------------------------------------



More information about the Python-list mailing list