Python实现获取文件夹中的最新文件

实现代码如下:

#获取某文件夹中的最新文件
import os
def find_newfile(path):
    #获取文件夹中的所有文件
    lists = os.listdir(path)
    #对获取的文件根据修改时间进行排序
    lists.sort(key=lambda x:os.path.getmtime(path +'\\'+x))
    #把目录和文件名合成一个路径
    file_newest = os.path.join(path,lists[-1])
    return file_newest
if __name__ =='__main__':
    newfile = find_newfile('..\\xc_datas')
    print(newfile)

猜你喜欢

转载自www.cnblogs.com/badbadboyyx/p/11943449.html