I am using the below code to get access to the last modified image file in a folder.
Since 3-4 images will be uploaded within a span of minute, I want to get access all the 3-4 files and rename it. I want the same process to run as soon as the new images are added to the folder.
import pandas as pd
import glob
import os
import os.path
#Find the last updated Image
folder_path = r'C:\Users\folder\path\for\images'
file_type = '\*jpg'
files = glob.glob(folder_path + file_type)
max_file = max(files, key = os.path.getctime)
print(max_file)
#Load the last updated engine number
df = pd.read_excel("g-ng.xlsx")
engine_no = df['engine_no'].iloc[-1]
print(engine_no)
#create new name for the file
new_fol_path = r'C:\Users\folder\path\for\images' "\\"
new_name = new_fol_path + engine_no + ".jpg"
print(new_name)
renamed = os.rename(max_file,new_name)
print(renamed)