I have been running a script that will will delete archive folders that are older than 14 days. I have been doing created. However there are times when the script will not run and my boses want the folders deleted but the name of the folder. The folders names are 28-Feb-12, etc.... How do use the name to subtract 14 days?

Use datetime module

>>> from datetime import datetime, timedelta
>>> fm = "%d-%b-%y"
>>> delta = timedelta(days=14)
>>> d = datetime.strptime("28-FEB-12", fm)
>>> (d - delta).strftime(fm)
'14-Feb-12'

This should give you some hints ...

# use Python module datetime
# to check if a dated filename +14 days is older than todays date

import datetime as dt

# dated filename for test
file_str = "28-Feb-12"

# abbreviated month list
month_abv = ['Jan','Feb','Mar','Apr',
    'May','Jun','Jul','Aug',
    'Sep','Oct','Nov','Dec']

# convert to a string module datetime can handle
# eg. "28-Feb-12" converts to "2012-02-28"
raw_date = file_str.split('-')
print(raw_date)  # test

'''output >>>
['28', 'Feb', '12']
'''

day = int(raw_date[0])
month = month_abv.index(raw_date[1]) + 1
year = int(raw_date[2]) + 2000

file_date = dt.date(year, month, day)
print(file_date, type(file_date))  # test

'''output >>>
(datetime.date(2012, 2, 28), <type 'datetime.date'>)
'''

# add 14 days to a given date
delta = dt.timedelta(days=14)
add14 = file_date + delta
print(add14, type(add14))  # test

'''output >>>
(datetime.date(2012, 3, 13), <type 'datetime.date'>)
'''

# is add14 older than todays date?
today = dt.date.today()
if add14 < today:
    print("%s is older than %s" %(add14, today))

'''possible output >>>
2012-03-13 is older than 2012-06-01
'''

# optionally
# convert datetime type back to file name string
# eg. "2012-03-13" converts to "13-Mar-12"
date14 = add14.strftime("%d-%b-%y")
print(date14, type(date14))

'''output >>>
('13-Mar-12', <type 'str'>)
'''

You can simplify your code using the hints Gribouillis gave you.

Edited 4 Years Ago by vegaseat

I knew about date time. I just didnt realize that the strftime was smart enough to take in account that not all months are the same length. What I get for just now taking up python ;)

Below is the code that works for what I want.

from datetime import datetime, timedelta
fm = "%d-%b-%y"
delta = timedelta(days=14)
d = datetime.strptime("28-FEB-12", fm)
(d - delta).strftime(fm)

How would I use this in a if statement correctly (I have the basic knowledge of what to do, but alas the syntax fails me)

IE:

import time
from datetime import datetime, timedelta
fname = "10-FEB-12"  #this is picked up by an os.listdir(".") in a for loop

fm = "%d-%b-%y"
delta = timedelta(days=14)
current_time = time.strftime("%d-%b-%y",time.localtime())

dir_date = datetime.strptime(fname, fm)
Del_time = (current_time - delta).strftime(fm)


if  dir_date >= Del_time:
    print "Folder %s is older than 14 days" % fname

You must not compare strings but datetime instances

now_date = datetime.now()
dir_date = datetime.strptime(fname, fm)
del_date = now_date - timedelta(days = 14)

if  dir_date <= del_date :
    print "Folder %s is older than 14 days" % fname
Comments
nice
This question has already been answered. Start a new discussion instead.