2

I am not getting the output in order while i am sorting the list of files in directory

import os
arr = sorted(os.listdir(r'D:\\Krish\\syn\\Python\\Washington'))
print(arr)

below is the output:

['1.pdf', '10.pdf', '11.pdf', '12.pdf', '2.pdf', '3.pdf', '4.pdf', '5.pdf', '6.pdf', '7.pdf', '8.pdf', '9.pdf']
2
  • 5
    It is sorted... only in lexicographic order, same as you would get in Windows Explorer. If you want the 10 to go after the 9 you would have to rename your files as '01.pdf', '02.pdf' and so on (or to specify a key to sorted to change the sorting criteria) Commented Apr 5, 2022 at 10:20
  • 1
    Answers have been given that explain how you could do this for the data you've shown. However, for a truly robust approach, you need to think about the possibility that files may exist in the directory that do not begin with numbers Commented Apr 5, 2022 at 10:31

3 Answers 3

2

if you want to sort as integer. it works

sorted(arr, key=lambda filename: int(filename.split('.')[0]))

the output is:

['1.pdf', '2.pdf', '3.pdf', '4.pdf', '5.pdf', '6.pdf', '7.pdf', '8.pdf', '9.pdf', '10.pdf', '11.pdf', '12.pdf']
Sign up to request clarification or add additional context in comments.

Comments

1

This is how strings are sorted. I understand what you want to do, and that can be done with a custom comparator.

def compare(item1, item2):
    return int(item1.split(".")[0]) - int(item2.split(".")[0])

arr = ['1.pdf', '10.pdf', '11.pdf', '12.pdf', '2.pdf', '3.pdf', '4.pdf', '5.pdf', '6.pdf', '7.pdf', '8.pdf', '9.pdf']

from functools import cmp_to_key
sorted_arr = sorted(arr, key=cmp_to_key(compare))
print(sorted_arr)

This gives:

['1.pdf', '2.pdf', '3.pdf', '4.pdf', '5.pdf', '6.pdf', '7.pdf', '8.pdf', '9.pdf', '10.pdf', '11.pdf', '12.pdf']

Comments

1

As stated in the comments, because the file names are strings, the list is sorted lexicographic.

You can try this:

arr = sorted(os.listdir(r'D:\Krish\syn\Python\Washington'), key=lambda f: int(f[:f.index('.')]))

The key argument is the function that the sorted function uses to determine the order.

In the code above, the function I pass takes all the characters until the period and converts them to an integer.

So "10.pdf" will be converted to 10 (as an integer)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.