import glob
import os
list_of_files = glob.glob('/path/to/folder/*')
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file
This code prints the file path as well, which is not required. I just need the latest file name.
import glob
import os
list_of_files = glob.glob('/path/to/folder/*')
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file
This code prints the file path as well, which is not required. I just need the latest file name.
list_of_files = '//x.x.x./y$/Process/Daily_Mismatch_Report/'
def get_latest_file(path, *paths): """Returns the name of the latest (most recent) file of the joined path(s)""" fullpath = os.path.join(path, paths) files = glob.glob(fullpath) if not files: return None latest_file = max(files, key=os.path.getctime) filename = os.path.split(latest_file) return filename p,f= get_latest_file(list_of_files,'.txt') print(f)