Reputation: 23
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']
Upvotes: 2
Views: 115
Reputation: 1040
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)
Upvotes: 1
Reputation: 1183
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']
Upvotes: 1
Reputation: 51
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']
Upvotes: 2