I am still reading the learing python o'reilly book and not sure the best way to approch my problem.
Given c:\dir1\dir2\dir3.
I want to zip all files in dir3 if those files are older than 30 days using 1 zip file (ie. dir_3_files.zip). If all files in dir3 are older 30 days, I want to zip the directory (ie. dir3.zip). I want to recursively keep doing this until I reach the top.
This is what I have so far, but I don't know where to interrupt os.walk to work with files in a directory. I hope this makes sense.
import os, os.path, stat, time
from datetime import date, timedelta
dirsNotUsed = []
def getdirs(basedir, age):
for root, dirs, files in os.walk(basedir):
basedate, lastused = datecheck(root, age)
if lastused < basedate: #Gets files older than (age) days
dirsNotUsed.append(root)
def datecheck(root, age):
basedate = date.today() - timedelta(days=age)
used = os.stat(root).st_mtime # st_mtime=modified, st_atime=accessed
year, day, month = time.localtime(used)[:3]
lastused = date(year, day, month)
return basedate, lastused
def archive():
pass
def main():
basedir = raw_input('Choose directory to scan: ')
age = raw_input('Only scan files older than... (days): ')
getdirs(basedir, int(age))
if __name__ == '__main__':
main()