Adam,

Look at the source for the shutil class. You may be able to adjust
shutil.rmtree to fix your problems.

Martin Katz, Ph.D.

-----Original Message-----
From: Adam Getchell [mailto:[EMAIL PROTECTED]] 
Sent: Sunday, May 12, 2002 1:50 AM
To: [EMAIL PROTECTED]
Subject: Shell tool


Hello all,

I've just started trying to use Python to automate Windows system
administration. I wrote my first (long-winded, amateurish) shell-tool to
os.path.walk through a directory structure and delete files older than 2
days. (Keeps users from cluttering some temporary storage space). It works
fine, except that it does not delete the empty directories. Any hints?

"""Delete files older than AGING constant using os.path.walk"""
import sys, os, time

AGING = 172800  # Default 2 days

def lister(dummy, dirName, filesInDir):
    print '[' + dirName + ']'
    for fname in filesInDir:
        path = os.path.join(dirName, fname)
        if not os.path.isdir(path):
            print path, time.ctime(os.stat(path).st_mtime), fileage(path),
prune(path)
            purge(path)

def fileage(file):
    curtime = time.time()
    modtime = os.stat(file).st_mtime
    age = curtime - modtime
    return age

def prune(file):
    if fileage(file) > AGING:
        return ("T")
    else:
        return ("File should not be deleted")

def purge(file):
    if prune(file) == "T":
        os.remove(file)        
    

if __name__ == '__main__':
    os.path.walk(sys.argv[1], lister, None)

--Adam Getchell
_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython

Reply via email to