-3
    for f in os.listdir(path):
        print(f)
    

results in:

    TSR23_kaji_v004_10.png
    TSR23_kaji_v004_100.png
    TSR23_kaji_v004_1000.png
    TSR23_kaji_v004_11.png
    TSR23_kaji_v004_12.png
    TSR23_kaji_v004_13.png
    TSR23_kaji_v004_14.png
    TSR23_kaji_v004_15.png
    TSR23_kaji_v004_16.png
    TSR23_kaji_v004_200.png
    TSR23_kaji_v004_99.png

here my main problem is it doesn't sorts according to the digit in the last before the extension ".png"

I want to have output as follows:

    TSR23_kaji_v004_10.png
    TSR23_kaji_v004_11.png
    TSR23_kaji_v004_12.png
    TSR23_kaji_v004_13.png
    TSR23_kaji_v004_14.png
    TSR23_kaji_v004_15.png
    TSR23_kaji_v004_16.png
    TSR23_kaji_v004_99.png
    TSR23_kaji_v004_100.png
    TSR23_kaji_v004_200.png
    TSR23_kaji_v004_1000.png

Please guide me to get the results

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 24 '22 at 03:53
  • You are seeing the results in alphabetic order, not numeric order. '10' < '1000' < '11'. This is the standard order on every OS that I know of. – Frank Yellin May 24 '22 at 03:54

2 Answers2

0

You need a way to extract the last number and convert it from a string to a number. A regular expression is one way to extract the text. For example, one or more digits followed by a .png character can be found with:

import re

def get_last_int(s):
    return int(re.search(r'\d+(?=\.png)', s).group())

Passing it one of your strings gives the int:

get_last_int('TSR23_kaji_v004_200.png')
# 200

With that, you can use sorted() to sort based on the integers in question:

import re


def get_last_int(s):
    return int(re.search(r'\d+(?=\.png)', s).group())

l = ['TSR23_kaji_v004_10.png',
 'TSR23_kaji_v004_100.png',
 'TSR23_kaji_v004_1000.png',
 'TSR23_kaji_v004_11.png',
 'TSR23_kaji_v004_12.png',
 'TSR23_kaji_v004_13.png',
 'TSR23_kaji_v004_14.png',
 'TSR23_kaji_v004_15.png',
 'TSR23_kaji_v004_16.png',
 'TSR23_kaji_v004_200.png',
 'TSR23_kaji_v004_99.png']


sorted(l, key=get_last_int)
# ['TSR23_kaji_v004_10.png',
#  'TSR23_kaji_v004_11.png',
#  'TSR23_kaji_v004_12.png',
#  'TSR23_kaji_v004_13.png',
#  'TSR23_kaji_v004_14.png',
#  'TSR23_kaji_v004_15.png',
#  'TSR23_kaji_v004_16.png',
#  'TSR23_kaji_v004_99.png',
#  'TSR23_kaji_v004_100.png',
#  'TSR23_kaji_v004_200.png',
#  'TSR23_kaji_v004_1000.png']
Mark
  • 90,562
  • 7
  • 108
  • 148
0

Use natsort:

# pip install natsort
from natsort import natsorted

l = ['TSR23_kaji_v004_10.png',
 'TSR23_kaji_v004_100.png',
 'TSR23_kaji_v004_1000.png',
 'TSR23_kaji_v004_11.png',
 'TSR23_kaji_v004_12.png',
 'TSR23_kaji_v004_13.png',
 'TSR23_kaji_v004_14.png',
 'TSR23_kaji_v004_15.png',
 'TSR23_kaji_v004_16.png',
 'TSR23_kaji_v004_200.png',
 'TSR23_kaji_v004_99.png']


out = natsorted(l)

Output:

['TSR23_kaji_v004_10.png',
 'TSR23_kaji_v004_11.png',
 'TSR23_kaji_v004_12.png',
 'TSR23_kaji_v004_13.png',
 'TSR23_kaji_v004_14.png',
 'TSR23_kaji_v004_15.png',
 'TSR23_kaji_v004_16.png',
 'TSR23_kaji_v004_99.png',
 'TSR23_kaji_v004_100.png',
 'TSR23_kaji_v004_200.png',
 'TSR23_kaji_v004_1000.png']
mozway
  • 194,879
  • 13
  • 39
  • 75