Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading