leemengtaiwan
10/17/2017 - 2:23 PM

將資料夾的檔案依照更新時間排序回傳檔案位置

import os.path, time
UPLOAD_FOLDER = 'static/uploaded_images'

# exclude .json file
files = ['/'.join((UPLOAD_FOLDER, file)) \
    for file in os.listdir(UPLOAD_FOLDER) if 'json' not in file]
    
last_modified_files = [(file, os.path.getmtime(file)) for file in files]
last_modified_files = sorted(last_modified_files, key=lambda t: t[1], reverse=True)