【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