Naturally sort Path in Python

sort path

If you want to sort the path naturally (sort according to the numbers), you can put a lambda expression in key as shown below.


natsorted(list(pathlib.Path(path).glob("*")),key=lambda x:x.name) 

The following digression

If you sort by path, it will be a dictionary sort

It was necessary to sort the image data when converting a large number of images to PDF. The name of the image file looks like this.

1.jpg
200.jpg
12.jpg
...

I want to sort them according to the numbers, so I want to sort them naturally instead of sorting them in a dictionary. When I got it with pathlib and sorted it with natsort, it looked like this.

PosixPath('/Users/usrname/images/1.jpg'),
PosixPath('/Users/usrname/images/10.jpg'),
PosixPath('/Users/usrname/images/20.jpg'),
...

No, please work with natsort. I thought, but it doesn't seem to sort if it's path. However, the countermeasures were written firmly in the reference.

Excerpt from natsort reference.py


>>> a = ['apple2.50', '2.3apple']
>>> natsorted(a, key=lambda x: x.replace('apple', ''), alg=ns.REAL)
['2.3apple', 'apple2.50']

It seems good to specify key in the same way as sort. The file name to be sorted is thrown. This is well written in the pathlib reference,

Excerpt from pathlib reference.py


>>> PurePosixPath('my/library/setup.py').name
'setup.py'

In other words, .name can take the end of path. If you use them in combination, you can sort them. The formula is omitted because it is at the beginning. When I tried using it, it looked like this.

PosixPath('/Users/usrname/images/1.jpg'),
PosixPath('/Users/usrname/images/2.jpg'),
PosixPath('/Users/usrname/images/3.jpg'),
...

It was sorted well. Congratulations

Recommended Posts

Naturally sort Path in Python
Custom sort in Python3
python in mongodb in descending sort
Sort by date in python
[Python] Sort
Python # sort
File / folder path manipulation in Python
Get the desktop path in Python
Calculate free-space path loss in Python
Get the script path in Python
Sort large text files in Python
Get the desktop path in Python
Project Euler # 15 "Lattice Path" in Python
When specifying multiple keys in python sort
Quadtree in Python --2
Python in optimization
CURL in python
New in Python 3.9 (2)-Sort directed acyclic graphs in Python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Implemented Stooge sort in Python3 (Bubble sort & Quicksort)
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
[Python] Sort the list of pathlib.Path in natural sort
[CpawCTF] Q14. [PPC] Try writing Sort! In Python