If you aren't familiar with setting up a chrontab you can set those up by following this (I looked for a mac-specific one but there are zillions of guides about this.)
Your archiving script can be quite simple. Begin by generating a list of files to archive (refer to this answer.)
import datetime as dt
import time
import os
import zipfile
now = dt.datetime.now()
ago = now-dt.timedelta(days=1)
Unlike in the referenced answer we want a list of files, not a print
ed output. So we initialize a list and append
to it at each iteration. Here I assume that your script lives in your downloads folder, but you can change the path given to os.walk
as appropriate.
to_archive = []
for root, dirs,files in os.walk('.'):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago:
to_archive.append(path)
We also want a strftime
representation of now
for our archive file title.
ts = now.strftime("%m_%d_%Y")
Finally write the files into an archive file (in this case I chose zip
format).
with zipfile.ZipFile('{}_archive.zip'.format(ts), 'w') as myzip:
for x in to_archive:
myzip.write(x)