Skript, mis otsib üles failid mille poole ei ole ammu pöördutud ja arhiveerib - Python

From ICO wiki
#!/usr/bin/python
# Lauri Liibert AK21 2010
# archive old files script

import os, time
import datetime
import tarfile
from optparse import OptionParser

# current dir (default path)
curdir = os.getcwd()
# today date
today = datetime.datetime.today()
# archive date pattern
archive_date =  today.strftime("%Y%m%d")
# default archive name with pattern
archive_template = "archive_" + archive_date + ".tar.gz"

# using (help)
parser = OptionParser()
parser.add_option("-p", "--path", dest="dirname", default=curdir,
                  action="store",
                  help="path", metavar="DIR")
parser.add_option("-a", "--archive",
                  action="store", dest="archive", default=archive_template,
                  help="archive name")
parser.add_option("-t", "--time", dest="time", default="356",
                  action="store", metavar="DAYS",
                  help="time", type="int")
parser.add_option("-r", "--remove", dest="remove", action="store_true",)
(options, args) = parser.parse_args()

# optargs
day_diff = options.time         #time diff in deys
path = options.dirname          # lookup path
archive = options.archive       # archive name
remove = options.remove         # remove files after archive

# tar with compressing
tar = tarfile.open(archive, "w:gz")

# walk all dir and subdirs
for root, dirs, files in os.walk(path):
# loop all files
    for f in files:
# try get file stats
        try:
# each file stats (mtime) 
            file = os.path.join(root,f)
            stats = os.stat(file)
            file_mtime =  stats.st_mtime

# file mtime date
            file_date = datetime.datetime.fromtimestamp(file_mtime)
#diff in days
            days = today - file_date
# days to int
            days_int = days.days.__int__()
# compare file and today
            if days_int > day_diff:
                print file, days_int, "day(s) old"
# add file to tar
                tar.add(file)
# if has remove argument
                if remove:
                    os.remove(file)
# if error
        except (IOError, OSError):
            print "Faili:", file, "ei saanud lugeda!"
# close archive file
tar.close()