1. 程式人生 > >【python】獲取目錄下的最新文件夾/文件

【python】獲取目錄下的最新文件夾/文件

tdi 所有 pat pytho 時間 body get color span

直接上代碼

 1 def new_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 return file_new 8 if __name__=="__main__": 9 test_report="path"#目錄地址 10 new_report(test_report)

【python】獲取目錄下的最新文件夾/文件