Как мне заставить сортировку Python вести себя как sort -n
из GNU coreutils?
Это мой images.txt
:
Vol. 1/Vol. 1 - Special 1/002.png
Vol. 1/Chapter 2 example text/002.png
Vol. 1/Vol. 1 Extra/002.png
Vol. 1/Chapter 2 example text/001.png
Vol. 1/Vol. 1 Extra/001.png
Vol. 1/Chapter 1 example text/002.png
Vol. 1/Vol. 1 - Special 1/001.png
Vol. 1/Chapter 1 example text/001.png
Когда я запускаю этот скрипт Bash:
#!/bin/bash
cat images.txt | sort -n
Я получаю следующий вывод:
Vol. 1/Chapter 1 example text/001.png
Vol. 1/Chapter 1 example text/002.png
Vol. 1/Chapter 2 example text/001.png
Vol. 1/Chapter 2 example text/002.png
Vol. 1/Vol. 1 Extra/001.png
Vol. 1/Vol. 1 Extra/002.png
Vol. 1/Vol. 1 - Special 1/001.png
Vol. 1/Vol. 1 - Special 1/002.png
Но когда я запускаю этот скрипт на Python:
#!/usr/bin/env python3
images = []
with open("images.txt") as images_file:
for image in images_file:
images.append(image)
images = sorted(images)
for image in images:
print(image, end="")
Я получаю следующий вывод, который мне не нужен:
Vol. 1/Chapter 1 example text/001.png
Vol. 1/Chapter 1 example text/002.png
Vol. 1/Chapter 2 example text/001.png
Vol. 1/Chapter 2 example text/002.png
Vol. 1/Vol. 1 - Special 1/001.png
Vol. 1/Vol. 1 - Special 1/002.png
Vol. 1/Vol. 1 Extra/001.png
Vol. 1/Vol. 1 Extra/002.png
Как мне добиться того же результата с Python, что и с Bash и sort -n
?