Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort filenames in directory in ascending order [duplicate]

Tags:

python

sorting

I have a directory with jpgs and other files in it, the jpgs all have filenames with numbers in them. Some may have additional strings in the filename.

For example.

01.jpg 

Or it could be

Picture 03.jpg 

In Python I need a list of all the jpgs in ascending order. Here is the code snippet for this

import os import numpy as np  myimages = [] #list of image filenames dirFiles = os.listdir('.') #list of directory files dirFiles.sort() #good initial sort but doesnt sort numerically very well sorted(dirFiles) #sort numerically in ascending order  for files in dirFiles: #filter out all non jpgs     if '.jpg' in files:         myimages.append(files) print len(myimages) print myimages 

What I get is this

['0.jpg', '1.jpg', '10.jpg', '11.jpg', '12.jpg', '13.jpg', '14.jpg',  '15.jpg', '16.jpg', '17.jpg', '18.jpg', '19.jpg', '2.jpg', '20.jpg',  '21.jpg', '22.jpg', '23.jpg', '24.jpg', '25.jpg', '26.jpg', '27.jpg',  '28.jpg', '29.jpg', '3.jpg', '30.jpg', '31.jpg', '32.jpg', '33.jpg',  '34.jpg', '35.jpg', '36.jpg', '37.jpg', '4.jpg', '5.jpg', '6.jpg',  '7.jpg', '8.jpg', '9.jpg'] 

Clearly it sorts blindly the most significant number first. I tried using sorted() as you can see hoping that it would fix it but it makes no difference.

like image 799
ChumbiChubaGo Avatar asked Oct 15 '15 21:10

ChumbiChubaGo


People also ask

How do I sort the names of files in a folder?

Icon view. To sort files in a different order, click the view options button in the toolbar and choose By Name, By Size, By Type, By Modification Date, or By Access Date. As an example, if you select By Name, the files will be sorted by their names, in alphabetical order.

How do I sort files by name in descending order?

To sort file names or other columns (such as Date Modified) in Windows Explorer or in any Office dialog based on Explorer (Open, Save As, etc.), click on the column's title. One click will sort in ascending order, and the next click will reverse it to descending order, and so back and forth. Was this reply helpful?

How do you sort a directory in Python?

Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.

How to sort a list of numbers in ascending order?

sorted_numbers = sorted ( [77, 22, 9, -6, 4000]) print ("Sorted in ascending order: ", sorted_numbers) The sorted () method also takes in the optional key and reverse arguments. In this example, we have a list of numbers sorted in descending order. reverse=True tells the computer to reverse the list from largest to smallest.

How to change sort order of files or folders in Linux?

By default DIR command displays the files or folders by sort order in ascending order by name of the files or folders. Below commands produce the same result. C:\> dir. and. C:\> dir /on. How to change this sort order? You need to prefix the symbol “-” before the parameters to reverse the sort order.

How to change the sort order of files in dir command?

By default DIR command displays the files or folders by sort order in ascending order by name of the files or folders. Below commands produce the same result. How to change this sort order? You need to prefix the symbol “-” before the parameters to reverse the sort order.

How to sort a list of names in Python?

names = ["Jessica", "Ben", "Carl", "Jackie", "Wendy"] print ("Unsorted: ", names) names.sort (reverse=True) print ("Sorted: ", names) This method will return a new sorted list from an iterable. Examples of iterables would be lists, strings, and tuples.


2 Answers

Assuming there's just one number in each file name:

>>> dirFiles = ['Picture 03.jpg', '02.jpg', '1.jpg'] >>> dirFiles.sort(key=lambda f: int(filter(str.isdigit, f))) >>> dirFiles ['1.jpg', '02.jpg', 'Picture 03.jpg'] 

A version that also works in Python 3:

>>> dirFiles.sort(key=lambda f: int(re.sub('\D', '', f))) 
like image 110
Stefan Pochmann Avatar answered Oct 03 '22 12:10

Stefan Pochmann


there is a module natsort. Just do pip install natsort.

>>> import natsort  >>> ll = ['Picture 13.jpg', 'Picture 14.jpg', 'Picture 15.jpg','Picture 0.jpg', 'Picture 1.jpg', 'Picture 10.jpg', 'Picture 11.jpg', 'Picture 12.jpg',  'Picture 16.jpg', 'Picture 17.jpg', 'Picture 18.jpg', 'Picture 19.jpg', 'Picture 2.jpg', 'Picture 20.jpg', 'Picture 21.jpg', 'Picture 22.jpg', 'Picture 23.jpg', 'Picture 24.jpg', 'Picture 25.jpg', 'Picture 26.jpg', 'Picture 27.jpg', 'Picture 28.jpg', 'Picture 29.jpg', 'Picture 3.jpg', 'Picture 30.jpg', 'Picture 31.jpg', 'Picture 32.jpg', 'Picture 33.jpg', 'Picture 34.jpg', 'Picture 35.jpg', 'Picture 36.jpg', 'Picture 37.jpg']          >>> print(natsort.natsorted(ll,reverse=True)) ['Picture 37.jpg', 'Picture 36.jpg', 'Picture 35.jpg', 'Picture 34.jpg', 'Picture 33.jpg', 'Picture 32.jpg', 'Picture 31.jpg', 'Picture 30.jpg', 'Picture 29.jpg', 'Picture 28.jpg', 'Picture 27.jpg', 'Picture 26.jpg', 'Picture 25.jpg', 'Picture 24.jpg', 'Picture 23.jpg', 'Picture 22.jpg', 'Picture 21.jpg', 'Picture 20.jpg', 'Picture 19.jpg', 'Picture 18.jpg', 'Picture 17.jpg', 'Picture 16.jpg', 'Picture 15.jpg', 'Picture 14.jpg', 'Picture 13.jpg', 'Picture 12.jpg', 'Picture 11.jpg', 'Picture 10.jpg', 'Picture 3.jpg', 'Picture 2.jpg', 'Picture 1.jpg', 'Picture 0.jpg'] 
like image 30
LetzerWille Avatar answered Oct 03 '22 12:10

LetzerWille