pathlib_helpers.py 658 B

12345678910111213141516171819202122232425262728
  1. import pathlib as pl
  2. import re
  3. def find_latest_file_matching_pattern(pattern, folder):
  4. folder_path = pl.Path(folder)
  5. modification_time_filename_dict = {}
  6. for child in folder_path.iterdir():
  7. pattern_matches = re.fullmatch(pattern, child.name, flags=re.I) is not None
  8. if child.is_file() and pattern_matches:
  9. modification_time_filename_dict[child.stat().st_mtime] = str(child)
  10. if len(modification_time_filename_dict) == 0:
  11. return None
  12. else:
  13. mtimes_sorted = sorted(modification_time_filename_dict.keys(), reverse=True)
  14. return modification_time_filename_dict[mtimes_sorted[0]]