一番最新ファイル
# 最新画像を1枚取り出す
def find_latest_file_with_number(filenames, search_number):
"""
This function finds the file with the largest numerical part in its name, given a specific number in the pattern.
"""
# Compile a regular expression to match the pattern (including the specific number) and extract the numerical part
regex = re.compile(rf"img_{search_number}_(\d+)\.jpg")
# Dictionary to hold the files and their numerical parts
file_dict = {}
# Extract the numerical part for each file that matches the pattern
for filename in filenames:
match = regex.match(filename)
if match:
file_dict[filename] = int(match.group(1))
# Find the file with the largest numerical part
if file_dict:
return max(file_dict, key=file_dict.get)
else:
return None