filename_list = os.listdir(path2) # 扫描目标路径的文件,将文件名存入列表
print(filename_list)
文件里排序整齐的文件名,经过os.listdir后会出现错乱,如下:
错误的输出示例:
filename_list = [
‘00796-1-1-1.m3u8’,
‘00796-1-1-2.m3u8’,
‘00796-1-1-3.m3u8’,
‘00796-1-2-1.m3u8’,
‘00796-1-2-2.m3u8’,
‘00796-1-2-3.m3u8’,
‘00796-1-2-4.m3u8’,
‘00796-1-2-5.m3u8’,
‘00796-10-1-1.m3u8’,
‘00796-10-1-2.m3u8’,
‘00796-10-1-3.m3u8’,
‘00796-10-1-4.m3u8’,
‘00796-10-1-5.m3u8’,
‘00796-10-2-1.m3u8’,
‘00796-2-1-1.m3u8’,
‘00796-2-1-2.m3u8’,
‘00796-2-1-3.m3u8’
]
有一个模块
pip install natsort 【适合只用数字作为名称的】
print(natsort.natsorted(filename_list ,reverse=True)) # reverse=True 反转