Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ##
- # clean_folder -- clean up temporary folders
- ##
- # Deletes everything under a folder which hasn't been modified
- # in a week. Deletes directories that are empty, too.
- ###
- import os, sys, time
- if (len(sys.argv) == 2) and (sys.argv[1] == '-d'):
- dryrun = True
- else:
- dryrun = False
- tmpdir = '/home/danny/tmp'
- daysback = 7
- cutofftime = time.time() - (60 * 60 * 24 * daysback)
- for d in os.walk(tmpdir, topdown=False):
- (dirpath, dirnames, filenames) = d
- for f in filenames:
- thisfile = os.path.join(dirpath, f)
- if (os.lstat(thisfile).st_mtime < cutofftime):
- try:
- if dryrun:
- print "I would delete:", thisfile
- else:
- os.remove(thisfile)
- except OSError, (errno, strerror):
- print "%s: OSError(%s): %s" % (d, errno, strerror)
- for d in dirnames:
- thisdir = os.path.join(dirpath, d)
- if not os.listdir(thisdir):
- try:
- if dryrun:
- print "I would delete:", thisfile
- else:
- os.rmdir(thisdir)
- except OSError, (errno, strerror):
- if (errno != 66):
- print "%s: OSError(%s): %s" % (d, errno, strerror)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement