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?
treyb
0
Newbie Poster
Recommended Answers
Jump to PostUse 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'
Jump to PostYou 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
All 5 Replies
Gribouillis
1,391
Programming Explorer
Team Colleague
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
treyb
0
Newbie Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
vegaseat
commented:
nice
+14
treyb
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.