python文件夹排序笔记

x33g5p2x  于2022-05-16 转载在 Python  
字(0.5k)|赞(0)|评价(0)|浏览(194)

natsort自然排序:

pip install natsort

if __name__ == '__main__':
    from natsort import natsorted

    x = ['41', '2', '11', '33']
    y=natsorted(x)

    print(x)
    print(y)

结果:

['41', '2', '11', '33']
['2', '11', '33', '41']

python文件夹时间排序

def new_report(test_report):
    lists =glob.glob(test_report+'/*.png')  # 列出目录的下所有文件和文件夹保存到lists
    print(lists)
    lists.sort(key=lambda filen: os.path.getmtime( filen))  # 按时间排序
    file_new = lists  # 获取最新的文件保存到file_new
    return file_new

if __name__ == '__main__':

    file_new = new_report(r"I:\project\3d\detect\Complex-YOLOv4-Pytorch-master\dataset\kitti\testing\image_2")
    print(file_new)

相关文章