File deletion after 72 hours of creation

A

ALEXURC

I'm looking for a simple method to delete a folder after 72 "Business
hours" (saturday/sunday doesnt count) since its creation. Note that
This is on a linux system and I realize that it will be the last
modified time. These files wont be modified since their creation.

Im very confused on how to work with the number easily.
 
S

skip

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
 
A

ALEXURC

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"
 
T

Tim Williams

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"

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)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top