Skip to content

Instantly share code, notes, and snippets.

@PSJoshi
Last active August 29, 2015 14:14
Show Gist options
  • Save PSJoshi/11643fb4231d943a2898 to your computer and use it in GitHub Desktop.
Save PSJoshi/11643fb4231d943a2898 to your computer and use it in GitHub Desktop.
Find latest modified files in python
def Latest_files(options, roots):
"""" A generator to enumerate the contents of 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
continue
for fn in filenames:
if any(fnmatch.fnmatch(fn, w) for w in options.exc_files):
continue
path = os.path.join(dirpath, fn)
stat = os.lstat(path)
mtime = max(stat.st_mtime, stat.st_ctime)
yield mtime, stat.st_size, path
I do not remember the source. I will update as soon as I spot it again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment