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 |
- RE: Shell tool Adam Getchell
- RE: Shell tool Richard Kemp
- Re: Shell tool Matthew Sherborne
- Re: Shell tool Patricia J. Hawkins
- Shell tool Adam Getchell
- RE: Shell tool Richard Kemp
- RE: Shell tool Martin Katz