简体   繁体   中英

How to have an ordered list of files with digits?

I have a folder of files and want to read the files one by one because it is frames of a video.

However, when I am trying to have an ordered list of files, it is ordered as follows:

data_dir = './data/'
filenames =listdir(data_dir)
N=len(filenames)
filenames.sort()
filenames 


['Image1.jpg',
 'Image10.jpg',
 'Image11.jpg',
 'Image12.jpg',
 'Image13.jpg',
 'Image14.jpg',
 'Image15.jpg',
 'Image2.jpg',
 'Image3.jpg',
 'Image4.jpg',
 'Image5.jpg',
 'Image6.jpg',
 'Image7.jpg',
 'Image8.jpg',
 'Image9.jpg']

How to have an ordered list of images based on the numbers?

Use regex to extract the numeric part and use custom sorting to sort accordingly you can also use regex like r"Image(\d+).jpg" to be precise.

import re
data_dir = './data/'
filenames =listdir(data_dir)
N=len(filenames)
filenames.sort(key=lambda filename : int(''.join(re.findall("\d+",filename))))
filenames

Use the sorted function with a lambda by key (this assumes all filenames contain "IMG"):

sorted_filenames = sorted(filenames, key= lambda x: int(x.split("IMG")[1].split(".")[0]))

Result:

['IMG1.bmp',
 'IMG2.bmp',
 'IMG3.bmp',
 'IMG4.bmp',
 'IMG5.bmp',
 'IMG6.bmp',
 'IMG7.bmp',
 'IMG8.bmp',
 'IMG9.bmp',
 'IMG10.bmp',
 'IMG11.bmp',
 'IMG12.bmp',
 'IMG13.bmp',
 'IMG14.bmp',
 'IMG15.bmp']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM