zoukankan      html  css  js  c++  java
  • 文件列表,根据修改时间排序

    code
    res.sort(key=lambda fn: os.path.getmtime(os.path.join(folder_storage,fn["name"])),reverse = True)
    # 方法一
    import os
    def listdir(path, list_name):
        """
        将文件路径和修改时间存入列表中
        :param path: 文件夹的绝对路径
        :param list_name: 空列表
        :return:
        """
        for file in os.listdir(path):  # 对目录中的所有文件进行遍历
            file_path = os.path.join(path, file)  # 文件的路径
            if os.path.isdir(file_path):  # 如果拼接后的还是目录,进行递归
                listdir(file_path, list_name)
            else:  # 如果拼接之后,还是文件,就将元组形式存到列表
                list_name.append((file_path, os.path.getmtime(file_path)))
     
    def newestfile(target_list):
        """
        找出最新修改的文件
        :param target_list: 存有文件路径和修改时间的列表
        :return: 最新的文件
        """
        newest_file = target_list[0]  # 默认第一个元组中的文件是最新的
        for i in range(len(target_list)):  # 对列表进行遍历
            if i < (len(target_list) - 1) and newest_file[1] < target_list[i + 1][1]:
                newest_file = target_list[i + 1]
            else:
                continue
        print('newest file is', newest_file)
        return newest_file
    
    p = r'G:	est_demo'
    list = []
    listdir(p, list)
    new_file = newestfile(list)
     
    
    # 方法二
    def new_report(test_report):
        lists = os.listdir(test_report)   # 列出目录的下所有文件和文件夹保存到lists
        lists.sort(key=lambda fn: os.path.getmtime(test_report + "/" + fn))  # 按时间排序
        file_new = os.path.join(test_report, lists[-1])     # 获取最新的文件保存到file_new
        return file_new
     
    file_new = new_report("G:\test_demo")
    print(file_new) 
     
     
     
     
     
     
     
     
     
     
     
     
     
     

  • 相关阅读:
    [译]HTML&CSS Lesson5: 定位
    Emscripten教程之入门指导
    html简单响应式滚动条置顶
    移动web开发问题和优化小结
    React+Webpack+ES6 兼容低版本浏览器(IE9)解决方案
    ECMAScript中有两种属性:数据属性和访问器属性。
    css写作建议和性能优化小结
    validator API文档
    HBase概念学习(九)HTablePool为何弃用?
    leetcode -day 15 Distinct Subsequences
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14192631.html
Copyright © 2011-2022 走看看