I'm creating pdf file from images but having problem with sorting the jpg files in numeric order I have 20 files from 1.jpg to 20.jpg I'm using below code to sort all files in order
import os
sorted(os.listdir('path/to/jpg/files'))
but it will print 1.jpg, 11.jpg, 12.jpg and so on.
Any ideas?
sorted
takes a key. You can use lambda
function in the key to do a numeric order sort.
Ex:
import os
sorted(os.listdir('path/to/jpg/files'), key=lambda x: int(x.split(".")[0]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With