天天看點

python讀取檔案夾下最新檔案_【python】擷取目錄下的最新檔案夾/檔案

1 defnew_report(test_report):2 lists = os.listdir(test_report) #列出目錄的下所有檔案和檔案夾儲存到lists

3 print(list)4 lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn))#按時間排序

5 file_new = os.path.join(test_report,lists[-1]) #擷取最新的檔案儲存到file_new

6 print(file_new)7 returnfile_new8 if __name__=="__main__":9 test_report="path"#目錄位址

10 new_report(test_report)