Google Answers Logo
View Question
 
Q: Python program to auto delete folders named with dates older than a period of ti ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Python program to auto delete folders named with dates older than a period of ti
Category: Computers > Programming
Asked by: keenr1214-ga
List Price: $15.00
Posted: 10 Sep 2004 20:52 PDT
Expires: 10 Oct 2004 20:52 PDT
Question ID: 399628
I am looking for a program written in Python that take the folders
that are inside a given directory (specified in script) and look at
their folder names.  The names of all the folders in this given
directory are assumed to be of the following format for a date
mm-dd-yy.  There will be a number of these folders which contain
subfolders and files.

I am looking for this program to take the dates of these folders and
delete the entire folder if its date is older than a specific period
of time.  I am looking to enter 1 month as this period of time.

If it helps to see what I am doing, please reference
http://answers.google.com/answers/threadview?id=399166 .
Answer  
Subject: Re: Python program to auto delete folders named with dates older than a period o
Answered By: leapinglizard-ga on 10 Sep 2004 23:29 PDT
Rated:5 out of 5 stars
 
Dear keenr1214,

The script follows. As before, there are constants to set at the
beginning, and there's an optional log file that can get fairly big
over time.

I'm anxious to meet all your needs and to win a five-star rating for
every answer I provide, so please let me know if you have any trouble
with the script or if there are additional details or program features
I should provide. I'm not satisfied until you're satisfied. For this
reason, I ask that you refrain from rating my answer until I've had a
chance to address all of your concerns.

leapinglizard




# begin sweeper.py

# This program identifies directories whose names are in the date
# format mm-dd-yy, and deletes them if they are older than a certain
# number of days.


# *** adjust constants as necessary ***

BASE_DIRECTORY = 'archive'     # search in this directory for old archives

MAXIMUM_DAYS_ALLOWED = 31      # delete archive folders older than this

LOG_FILE = 'sweeper_log.txt'   # record actions here; set to '' for no log


# *** end of constants ***




import sys, os, string, re
import time
import calendar

def msg(s):
    if LOG_FILE.strip() != '':
        open(LOG_FILE, 'a').write(s+'\n')
    print s

msg('[%s] starting sweeper.py' % time.asctime())

(cy, cm, cd) = time.localtime()[:3]

mm = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
      [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]]

def calspan((y0, m0, d0), (y1, m1, d1)):
    if y0 > y1:
        return -1
    if y0 == y1 and m0 > m1:
        return -1
    if (y0 == y1 and m0 == m1) and d0 > d1:
        return -1
    ct = 0
    while (y0 < y1 or m0 < m1) or d0 < d1:
        ct += 1
        d0 += 1
        if d0 <= mm[calendar.isleap(y0)][m0-1]:
            continue
        d0 = 1
        m0 += 1
        if m0 <= 12:
            continue
        m0 = 1
        y0 += 1
    return ct

for name in os.listdir(BASE_DIRECTORY):
    match = re.search('^(\d\d)-(\d\d)-(\d\d)$', name)
    if not match:
        continue
    fm = int(match.group(1))
    fd = int(match.group(2))
    fy = 2000+int(match.group(3))

    span = calspan((fy, fm, fd), (cy, cm, cd))
    if span <= MAXIMUM_DAYS_ALLOWED:
        if span < 0:
            msg('skipping "%s", which has a date in the future' % name)
        else:
            msg('skipping "%s", which is %d days old' % (name, span))
        continue

    msg('deleting "%s", which is %d days old' % (name, span))
    fname = '%s/%s' % (BASE_DIRECTORY, name)
    if os.path.isdir(fname):
        os.removedirs(fname)
    else:
        os.remove(fname)

msg('[%s] ending sweeper.py\n' % time.asctime())

# end sweeper.py

Request for Answer Clarification by keenr1214-ga on 11 Sep 2004 09:19 PDT
I am very sorry about the 4 star rating last time.  I fully intendes
to give 5 stars, and I am almost certain that when I made the
selection I selected 5 stars. Sorry about that.

I copied the script, however, I am recieving an error:

"
  File "C:\File Server\Backups\Scripts\sweeper.py", line 26
     def msg(s)
              ^
SyntaxError: invalid syntax
"
Please advise

Clarification of Answer by leapinglizard-ga on 11 Sep 2004 09:28 PDT
I've just tested the script again, and it's working fine. Something
must have gone wrong when you copied and pasted it. In particular, the
Python interpreter is telling you that there's a colon missing at the
end of the function header. But if you look at the script above, it
reads

def msg(s):

and not

def msg(s)

which would indeed be wrong. So that colon at the end of the line
somehow went missing. I suggest you start over with a fresh file, then
copy and paste the script anew. It really should run, since the Python
interpreter is telling me that the code is syntactically correct. If
you're still having trouble, let me know, and I'll post the actual
file to a website for you to download.

leapinglizard

Request for Answer Clarification by keenr1214-ga on 11 Sep 2004 10:12 PDT
Thank you, the program works.  Sorry, I must have deleted the colon
without realizing.

Clarification of Answer by leapinglizard-ga on 11 Sep 2004 10:25 PDT
I'm glad it works now. And thanks for the tip.

leapinglizard
keenr1214-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy