8

How to add files into a list by order

In my directory i have the following files: slide1.xml, slide2.xml, slide3.xml ... slide13.xml

os.listdir(path) doesn't return me a list by order

I've tried this way

files_list = [x for x in sorted(os.listdir(path+"/slides/")) if os.path.isfile(path+"/slides/"+x)]

output: ['slide1.xml', 'slide10.xml', 'slide11.xml', 'slide12.xml', 'slide13.xml', 'slide2.xml', 'slide3.xml', 'slide3_COPY.xml', 'slide4.xml', 'slide5.xml', 'slide6.xml', 'slide7.xml', 'slide8.xml', 'slide9.xml']

Pythonizer
  • 1,080
  • 4
  • 15
  • 25
  • I think it is in order, 'slide10.xml' goes before 'slide2.xml'. If you want 'slide2.xml' goes before 'slide10.xml' you need to implement a custom sort – Andres May 18 '14 at 17:14

2 Answers2

10

Sort by key:

import re
files = ['slide1.xml', 'slide10.xml', 'slide11.xml', 'slide12.xml', 'slide13.xml', 'slide2.xml', 'slide3.xml', 'slide3_COPY.xml', 'slide4.xml', 'slide5.xml', 'slide6.xml', 'slide7.xml', 'slide8.xml', 'slide9.xml']
ordered_files = sorted(files, key=lambda x: (int(re.sub('\D','',x)),x))

gives ['slide1.xml', 'slide2.xml', 'slide3.xml', 'slide3_COPY.xml', 'slide4.xml', 'slide5.xml', 'slide6.xml', 'slide7.xml', 'slide8.xml', 'slide9.xml', 'slide10.xml', 'slide11.xml', 'slide12.xml', 'slide13.xml']

Daniel
  • 42,087
  • 4
  • 55
  • 81
  • \d may have problem if your file name contains special character, for example Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩. See this http://stackoverflow.com/a/6479605/2157469 – Hao Aug 18 '16 at 08:39
1

You may want to use your own sort function

def custom_sort(x, y):
    pass
    #your custom sort

files_list = [x for x in sorted(os.listdir(path+"/slides/"), cmp=custom_sort) if os.path.isfile(path+"/slides/"+x)]

doc

also check natsort

Andres
  • 4,323
  • 7
  • 39
  • 53