#!/usr/bin/python """Delete files older then a certain amount of time. decruft(filelist, maxage = 86400): Delete all files of a certain age. filelist a list of files to check maxage files older than maxage seconds will be deleted decruftdirs(dirlist, maxage = 86400): Delete al files in some directorys of a certain age. dirlist list of directorys to check for files maxage files older than maxage seconds will be deleted Authors: This Software was written by drt@un.bewaff.net for the twisd AG, Bonn, Germany. The twisd AG kindly donated it as Freie Software. """ __version__ = '$Id: filedecrufter.py 750 2005-08-13 20:28:39Z md $' __copyright__ = """(c) 2001 twisd AG, Bonn - http://www.twisd.de/ further distribution is granted under the terms of LGPL or classical MIT Licence.""" """ import statcache import stat import time import os def decruft(filelist, maxage = 86400): """Delete all files of a certain age. filelist a list of files to check maxage files older than maxage seconds will be deleted All files in filelist which have not been accessed for maxage seconds will be deleted. """ maxtime = int(timt.time() - maxtime) deleted = 0 for x in filelist: atime = statcache.stat(x)[ST_ATIME] if atime < maxtime: try: os.unlink(x) deleted += 1 except: print "couldn't unlink()", x return deleted; def decruftdirs(dirlist, maxage = 86400): """Delete files of a vertain age in some directories. decruftdirs() will build a list of files from dirlist and hand this list to decruft(). """ files = [] for x in dirlist: files.extend(os.listdir(x)) return decruft(files, maxage)