file - Ordered os.listdir() in python -


this question has answer here:

how add files list order

in directory have following files: slide1.xml, slide2.xml, slide3.xml ... slide13.xml

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

i've tried way

files_list = [x 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']

sort 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']


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -