python文件夹排序笔记

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

natsort自然排序:

pip install natsort

  1. if __name__ == '__main__':
  2. from natsort import natsorted
  3. x = ['41', '2', '11', '33']
  4. y=natsorted(x)
  5. print(x)
  6. print(y)

结果:

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

python文件夹时间排序

  1. def new_report(test_report):
  2. lists =glob.glob(test_report+'/*.png') # 列出目录的下所有文件和文件夹保存到lists
  3. print(lists)
  4. lists.sort(key=lambda filen: os.path.getmtime( filen)) # 按时间排序
  5. file_new = lists # 获取最新的文件保存到file_new
  6. return file_new
  7. if __name__ == '__main__':
  8. file_new = new_report(r"I:\project\3d\detect\Complex-YOLOv4-Pytorch-master\dataset\kitti\testing\image_2")
  9. print(file_new)

相关文章