Skip to content

Instantly share code, notes, and snippets.

Last active January 21, 2016 04:44
  • Star 20 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
List the contents of folders recursively, sorted by modification time.
#! /usr/bin/env python
# License:
# See
import optparse
import os
import fnmatch
import time
# Parse options
parser = optparse.OptionParser(usage='Usage: %prog [options] path [path2 ...]')
parser.add_option('-g', action='store', type='long', dest='secs', default=10,
help='set threshold for grouping files')
parser.add_option('-f', action='append', type='string', dest='exc_files', default=[],
help='exclude files matching a wildcard pattern')
parser.add_option('-d', action='append', type='string', dest='exc_dirs', default=[],
help='exclude directories matching a wildcard pattern')
options, roots = parser.parse_args()
if len(roots) == 0:
print('You must specify at least one path.\n')
def iterFiles(options, roots):
""" A generator to enumerate the contents directories recursively. """
for root in roots:
for dirpath, dirnames, filenames in os.walk(root):
name = os.path.split(dirpath)[1]
if any(fnmatch.fnmatch(name, w) for w in options.exc_dirs):
del dirnames[:] # Don't recurse here
stat = os.stat(os.path.normpath(dirpath))
yield stat.st_mtime, '', dirpath # Yield directory
for fn in filenames:
if any(fnmatch.fnmatch(fn, w) for w in options.exc_files):
path = os.path.join(dirpath, fn)
stat = os.lstat(os.path.normpath(path)) # lstat fails on some files without normpath
mtime = max(stat.st_mtime, stat.st_ctime)
yield mtime, stat.st_size, path # Yield file
# Build file list, sort it and dump output
ptime = 0
for mtime, size, path in sorted(iterFiles(options, roots), reverse=True):
if ptime - mtime >= options.secs:
print('-' * 30)
timeStr = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime))
print('%s %10s %s' % (timeStr, size, path))
ptime = mtime
Copy link

This piece of code may be nice, but it has no license information attached?!?

Copy link

Added license information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment