How to sort file names in a particular order using python

后端 未结 2 1566
广开言路
广开言路 2021-02-08 07:52

Is there a simple way to sort files in a directory in python? The files I have in mind come in an ordering as

file_01_001
file_01_005
...
file_02_002
file_02_00         


        
2条回答
  •  天涯浪人
    2021-02-08 08:08

    A much better solution is to use Tcl's "lsort -dictionary":

    from tkinter import Tcl
    Tcl().call('lsort', '-dict', file_list)
    

    Tcl dictionary sorting will treat numbers correctly, and you will get results similar to the ones a file manager uses for sorting files.

提交回复
热议问题