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

From ICO wiki
Jump to navigationJump to search
(Created page with '#!/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 p…')
 
No edit summary
Line 1: Line 1:
<source lang="python">
#!/usr/bin/python
#!/usr/bin/python
# Lauri Liibert AK21 2010
# Lauri Liibert AK21 2010
Line 41: Line 42:


# walk all dir and subdirs
# walk all dir and subdirs
tar = tarfile.open(archive, "w:gz")
# walk all dir and subdirs
for root, dirs, files in os.walk(path):
# each file stats (mtime)
    for f in files:
        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)
</source>

Revision as of 01:44, 27 January 2011

#!/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
tar = tarfile.open(archive, "w:gz")

# walk all dir and subdirs
for root, dirs, files in os.walk(path):
# each file stats (mtime)
    for f in files:
        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)