# The following helps you find the files in a folder with a given pattern and find the latest modified file by checking the modified timestamp of the files
''' I store the modified time in a list so that i can do a max on the value
I store the timestamp and filename combination in a dictionary object so that i can lookup the filename of the file with the latest modified timestamp
Also, i am using the time in float format as the comparison is easy and accurate'''
def FindLatestFile(directory, lookforpattern):
timelist = []
filename_time = {}
try:
for file in os.listdir(directory):
if fnmatch.fnmatch(file, lookforpattern):
file_mod_time1 = os.stat(os.path.join(directory,file)).st_mtime
timelist.append(file_mod_time1)
filename_time[file_mod_time1] = file
print('LATEST FILE : ' + filename_time[max(timelist)])
except:
print('No file found with given pattern')
#USAGE:
FindLatestFile(r'C:\FOLDER1\outlog', 'file_name*.log')
No comments:
Post a Comment
Please write your comments and suggestions