zoukankan      html  css  js  c++  java
  • Python文件排序

    按文件名称字符串小写排序

    images.sort(key=lambda x: x.lower())

    按创建时间精确到秒排序

    images.sort(key=lambda x: os.path.getctime(x))

    按创建时间,精确到纳秒排序

    images.sort(key=lambda x: os.stat(x).st_ctime_ns)

    按文件名称去掉后缀的数值名称排序

    images.sort(key = lambda x: int(x[:-4]))

    使用os.stat的返回值statinfo的三个属性获取文件的创建时间等信息

    statinfo=os.stat("E:\A\314_noteach_20190315162753_1552638473_152.png")
    
    st_atime (访问时间), st_mtime (修改时间), st_ctime(创建时间)
    print(statinfo)
    
    
    print(statinfo.st_mtime)
    print(statinfo.st_ctime_ns)

    可按照文件名称排序,对字符串和数值混合的可以自定义实现混合排序

    # -*- coding: utf-8 -*-
    # @Time    : 2019/4/30 13:32
    # @Author  : shine
    # @File    : mix_sort.py
    """
    基于字符串数字混合排序的Python脚本
    """
    
    
    def is_number(s):
        try:
            float(s)
            return True
        except ValueError:
            pass
    
        try:
            import unicodedata
            unicodedata.numeric(s)
            return True
        except (TypeError, ValueError):
            pass
    
        return False
    
    
    def find_continuous_num(astr, c):
        num = ''
        try:
            while not is_number(astr[c]) and c < len(astr):
                c += 1
            while is_number(astr[c]) and c < len(astr):
                num += astr[c]
                c += 1
        except:
            pass
        if num != '':
            return int(num)
    
    
    def comp2filename(file1, file2):
        smaller_length = min(len(file1), len(file2))
        for c in range(0, smaller_length):
            if not is_number(file1[c]) and not is_number(file2[c]):
                if file1[c] < file2[c]:
                    return True
                if file1[c] > file2[c]:
                    return False
                if file1[c] == file2[c]:
                    if c == smaller_length - 1:
                        if len(file1) < len(file2):
                            return True
                        else:
                            return False
                    else:
                        continue
            if is_number(file1[c]) and not is_number(file2[c]):
                return True
            if not is_number(file1[c]) and is_number(file2[c]):
                return False
            if is_number(file1[c]) and is_number(file2[c]):
                if find_continuous_num(file1, c) < find_continuous_num(file2, c):
                    return True
                else:
                    return False
    
    
    def sort_list_by_name(lst):
        for i in range(1, len(lst)):
            x = lst[i]
            j = i
            while j > 0 and comp2filename(x, lst[j-1]):
                lst[j] = lst[j-1]
                j -= 1
            lst[j] = x
        return lst
  • 相关阅读:
    VUE中全局变量的定义和使用
    Pull Request 工作流——更高效的管理代码
    仓储repository概念
    Mysql存储过程历史表备份
    OpenStack一键安装
    VMware虚拟机设置Win10固定ip
    C#_NPOI_Excel各种设置
    pycharm修改镜像
    C#模拟POST上传文件帮助类(支持https、http)
    Windows安装RabbitMQ
  • 原文地址:https://www.cnblogs.com/shinelover/p/10797358.html
Copyright © 2011-2022 走看看