Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python sort file names with numbers

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

print(dirFiles)
# ['1.jpg', '02.jpg', 'Picture 03.jpg']
Comment

python sort file names with numbers

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))

# ['Picture 0.jpg', 'Picture 1.jpg', 'Picture 2.jpg', 'Picture 3.jpg', 'Picture 10.jpg', 'Picture 11.jpg', 'Picture 12.jpg', 'Picture 13.jpg', 'Picture 14.jpg', 'Picture 15.jpg', 'Picture 16.jpg', 'Picture 17.jpg', 'Picture 18.jpg', 'Picture 19.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 30.jpg', 'Picture 31.jpg', 'Picture 32.jpg', 'Picture 33.jpg', 'Picture 34.jpg', 'Picture 35.jpg', 'Picture 36.jpg', 'Picture 37.jpg']
Comment

PREVIOUS NEXT
Code Example
Python :: import py to exe 
Python :: pandas filter non nan 
Python :: flask docker 
Python :: How to use PatBlt in Python 
Python :: an array of dates python 
Python :: np.sort descending 
Python :: matplotlib axes limits 
Python :: how to create virtual environment 
Python :: convert categorical variable to numeric python 
Python :: How to to efficiently find the first index in a sorted array of distinct numbers that is equal to the value at that index? 
Python :: flatten an irregular list of lists 
Python :: how to python hack 2021 course 
Python :: Can only use .str accessor with string values! 
Python :: replace commas with spaces python 
Python :: save ml model using joblib 
Python :: discord python command alias 
Python :: how to rearrange list in python 
Python :: get hwid python 
Python :: virtual env in python 
Python :: django httpresponseredirect 
Python :: How to count occurences of a certain item in a numpy array 
Python :: numpy.datetime64 to datetime 
Python :: pandas read csv read all rows except one 
Python :: python request example 
Python :: python imread multiple images 
Python :: python pdf merger 
Python :: how to convert 24 hours to 12 hours in python 
Python :: how to add headers in csv file using python 
Python :: create folder python 
Python :: Goal Parser Python 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =