我有一個與特定模式匹配的檔案串列,我需要從中獲取最新和第二個最新檔案路徑。
我可以使用下面的代碼獲取最新檔案
import glob
import os
list_of_files = glob.glob('/home/yash/proj_dir/reference_data/*/FeaturesV3.txt')
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
有沒有辦法從上述檔案模式中獲取第二個最新檔案?
uj5u.com熱心網友回復:
您可以 sort list_of_files,然后使用 index[-2]來獲取倒數第二項:
list_of_files = glob.glob('/tmp/*.json')
latest_file = sorted(list_of_files, key=os.path.getctime)
print(latest_file[-2])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/413564.html
標籤:
