Fork me on GitHub

Python之查找某目录中最新的文件

记录一下这个方法,感觉很有用!>.<

 1 import os
 2 def find_newest_file(path_file):
 3     lists = os.listdir(path_file)
 4     lists.sort(key=lambda fn: os.path.getmtime(path_file +'\\'+fn))
 5     print(lists)
 6     file_newest = os.path.join(path_file,lists[-1])
 7     return file_newest
 8 if __name__ =='__main__':
 9     file_newest = find_newest_file(r'D:\pythoncode\login_test\report')
10     print(file_newest)
posted @ 2018-07-17 16:03  linux超  阅读(1063)  评论(0编辑  收藏  举报