This will traverse all of the subdirectories and do what I think you want it to do. I have added some print statements which is probably enough by itself to answer your question as is shows what the root, dirs, and files contain. If you want to limit it to 3 levels, then you will want to store root and the first 2 dirs and pass them as basedir and then just use the files for that particular directory. If you are on a Linux system, pipe the output to a file if there are a lot of files and dirs. It will be easier to read. There are other ways of doing this using an os.path.walk() callback but I assume you want to continue down this road as you are following the book.
import os, os.path, stat, time
from datetime import date, timedelta
dirsNotUsed = []
def getdirs(basedir, age):
for root, dirs, files in os.walk(basedir):
print "root =", root
print "dirs =", dirs
print "files =", files
found = 1
for file in files:
found_file = datecheck(root, file, age)
if not found_file : #At least one file is not old enough
found = 0
""" or backup all of the files that are old enough
if found_file:
backup_list.append(os.path.join(root, file))
"""
if found:
archive(root, files)
def datecheck(root, file, age):
basedate = date.today() - timedelta(days=age)
fname = os.path.join(root, file)
used = os.stat(fname).st_mtime # st_mtime=modified, st_atime=accessed
year, day, month = time.localtime(used)[:3]
lastused = date(year, day, month)
if lastused < basedate: #Gets files older than (age) days
return 1
return 0 # Not old enough
def archive(root, files):
for file in files:
fname=os.path.join(root, file)
print "archiving", fname
if __name__ == '__main__':
basedir = raw_input('Choose directory to scan: ')
age = raw_input('Only scan files older than... (days): ')
getdirs(basedir, int(age))
Last edited by woooee; May 30th, 2008 at 5:29 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
Offline 2,302 posts
since Dec 2006