Skip to content

Instantly share code, notes, and snippets.

@kasimte
Created February 2, 2021 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasimte/1a62221a9615701c3dfc8711b364918c to your computer and use it in GitHub Desktop.
Save kasimte/1a62221a9615701c3dfc8711b364918c to your computer and use it in GitHub Desktop.
Example of reading a directory and returning the latest file matching a string using python.
def latest_match_from(path, match):
# read the modification time
mtime = lambda f: os.stat(os.path.join(path, f)).st_mtime
# read all files from path and sort based on modification time
files = list(sorted(os.listdir(path), key=mtime))
# filter based on match
matches = [a for a in files if match in a]
# return the full path of the most recent match
return os.path.join(path, matches[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment