How to have an ordered list of files with digits?

Question:

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?

Asked By: S.EB

||

Answers:

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
Answered By: THUNDER 07

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']
Answered By: JacobIRR
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.