Search code examples
pythongloblistdir

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?


Solution

  • 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']