Created
February 2, 2021 14:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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