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


直接上代碼

 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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM