How do you sort files numerically?

前端 未结 6 1223
一生所求
一生所求 2020-11-28 07:01

First off, I\'m posting this because when I was looking for a solution to the problem below, I could not find one on stackoverflow. So, I\'m hoping to add a little bit to t

6条回答
  •  隐瞒了意图╮
    2020-11-28 07:43

    @April provided a good solution in How is Pythons glob.glob ordered? that you could try

    #First, get the files:
    import glob
    import re
    
    files = glob.glob1(img_folder,'*'+output_image_format)
    
    # Sort files according to the digits included in the filename
    files = sorted(files, key=lambda x:float(re.findall("(\d+)",x)[0]))
    

提交回复
热议问题