Read latest file with a filename contains string in python

  python, readfile

Assume that I have a list of file like this:

  • abc_name.txt
  • def_name.txt
  • ghj_name.txt
  • abcdefghj.txt
  • xyz.txt

I knew how to read latest file by using max(list_of_files, key=os.path.getmtime), but it could be any last created file.

I want to read file that contains ‘name’ in its name and this file was last created (for example "def_name.txt").

How can I do that in python?

Many thanks.

Source: Python Questions

LEAVE A COMMENT