Самый старый файл - первый:
import os
import glob
# sort by change date
def sort(path, ext="*.jpg"):
files = glob.glob(os.path.join(path, ext))
files.sort(key=os.path.getmtime)
return files
# rename files
def rename(path):
_sorted = sort(path)
for i, file_path in enumerate(_sorted, 1):
folder, old_name = os.path.split(file_path)
new_name = f"{i}.jpg"
new_path = os.path.join(folder, new_name)
os.rename(file_path, new_path)
print(f"Rename {old_name} to {new_name}")
if __name__ == "__main__":
# path to dir with images
folder_path = "/path/to/dir/images"
rename(folder_path)