How does Python get the list of files in the directory and sort them naturally

Time:2022-1-14

In the actual algorithm and program development, the scene we often need to face is batch operation of files in the same directory.

And most of the time, the files in the directory are regular. At the same time, I hope the program will process in a certain order.

Method 1

?
1
2
3
4
5
6
7
8
>>> import os
>>> fileList = os.listdir('TestDir')
>>> fileList
['test1.txt', 'test10.txt', 'test11.txt', 'test2.txt', 'test23.txt', 'test28.txt']
>>> fileList.sort(key= lambda x:int(x[4:-4]))
>>> fileList
['test1.txt', 'test2.txt', 'test10.txt', 'test11.txt', 'test23.txt', 'test28.txt']
>>>

Method 2

?
1
2
3
4
5
6
7
8
9
>>> import os
>>> import natsort
>>> fileList = os.listdir('TestDir')
>>> fileList
['test1.txt', 'test10.txt', 'test11.txt', 'test2.txt', 'test23.txt', 'test28.txt']
>>> fileList = natsort.natsorted(fileList)
>>> fileList
['test1.txt', 'test2.txt', 'test10.txt', 'test11.txt', 'test23.txt', 'test28.txt']
>>>

Supplement: Python sorting files in the directory

1. Sort by time

?
1
2
3
4
5
6
7
8
9
10
11
def get_file_list(file_path):
    dir_list = os.listdir(file_path)
    if not dir_list:
        return
    else:
        #Note that the lambda expression is used here to arrange the files in ascending order according to the last modification time
        # os. path. The getmtime () function is to get the last modification time of the file
        # os. path. The getctime () function gets the last creation time of the file
        dir_list = sorted(dir_list,key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
        # print(dir_list)
        return dir_list

2. Get the file path of the last modification time

?
1
2
3
4
5
6
7
import os
url='D:\PycharmProjects\Ambulance_Api\logs'
lists=os.listdir(url)
print(lists)
lists.sort(key=lambda fn: os.path.getmtime(url+'\\'+fn))
filepath=os.path.join(url,lists[-1])
print(filepath)

3. Sort by file name

1) Sort strings with the library function sorted (), whose objects are characters

2) Sort the numbers with the function sort (). Its object is numbers. If you read a file, you need to deal with it (mask the file suffix)

?
1
2
3
4
5
6
7
import os
img_path ='./img/'
img_list =sorted(os.listdir(img_path))    #File names are sorted alphabetically
img_nums =len(img_list)
for i in range(img_nums):
img_name = img_path + img_list[i]
print(img_name)

The anonymous function (key = lambda x: int (x [: – 4]) is used in the sort function to add the suffix ‘ Jpg ‘”Mask” (because’. JPG ‘is 4 characters, [: – 4] means from the file name to the penultimate character)

The above is my personal experience. I hope I can give you a reference, and I hope you can support developepper.