Python 按升序对目录中的文件名进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33159106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort filenames in directory in ascending order
提问by user3474042
I have a directory with jpgs and other files in it, the jpgs all have filenames with numbers in them. Some may have additional strings in the filename.
我有一个包含 jpg 和其他文件的目录,这些 jpg 都有带有数字的文件名。有些可能在文件名中有额外的字符串。
For example.
例如。
01.jpg
Or it could be
或者它可能是
Picture 03.jpg
In Python I need a list of all the jpgs in ascending order. Here is the code snippet for this
在 Python 中,我需要按升序列出所有 jpg 的列表。这是这个的代码片段
import os
import numpy as np
myimages = [] #list of image filenames
dirFiles = os.listdir('.') #list of directory files
dirFiles.sort() #good initial sort but doesnt sort numerically very well
sorted(dirFiles) #sort numerically in ascending order
for files in dirFiles: #filter out all non jpgs
if '.jpg' in files:
myimages.append(files)
print len(myimages)
print myimages
What I get is this
我得到的是这个
['0.jpg', '1.jpg', '10.jpg', '11.jpg', '12.jpg', '13.jpg', '14.jpg',
'15.jpg', '16.jpg', '17.jpg', '18.jpg', '19.jpg', '2.jpg', '20.jpg',
'21.jpg', '22.jpg', '23.jpg', '24.jpg', '25.jpg', '26.jpg', '27.jpg',
'28.jpg', '29.jpg', '3.jpg', '30.jpg', '31.jpg', '32.jpg', '33.jpg',
'34.jpg', '35.jpg', '36.jpg', '37.jpg', '4.jpg', '5.jpg', '6.jpg',
'7.jpg', '8.jpg', '9.jpg']
Clearly it sorts blindly the most significant number first. I tried using sorted()
as you can see hoping that it would fix it but it makes no difference.
显然,它首先盲目地对最重要的数字进行排序。我尝试使用sorted()
你可以看到的希望它会修复它但它没有区别。
采纳答案by Stefan Pochmann
Assuming there's just one number in each file name:
假设每个文件名中只有一个数字:
>>> dirFiles = ['Picture 03.jpg', '02.jpg', '1.jpg']
>>> dirFiles.sort(key=lambda f: int(filter(str.isdigit, f)))
>>> dirFiles
['1.jpg', '02.jpg', 'Picture 03.jpg']
A version that also works in Python 3:
也适用于 Python 3 的版本:
>>> dirFiles.sort(key=lambda f: int(re.sub('\D', '', f)))
回答by LetzerWille
there is a module natsort
. Just do pip install natsort
.
有一个模块natsort
。就做pip install natsort
。
>>> import natsort
>>> ll = ['Picture 13.jpg', 'Picture 14.jpg', 'Picture 15.jpg','Picture 0.jpg', 'Picture 1.jpg', 'Picture 10.jpg', 'Picture 11.jpg', 'Picture 12.jpg', 'Picture 16.jpg', 'Picture 17.jpg', 'Picture 18.jpg', 'Picture 19.jpg', 'Picture 2.jpg', 'Picture 20.jpg', 'Picture 21.jpg', 'Picture 22.jpg', 'Picture 23.jpg', 'Picture 24.jpg', 'Picture 25.jpg', 'Picture 26.jpg', 'Picture 27.jpg', 'Picture 28.jpg', 'Picture 29.jpg', 'Picture 3.jpg', 'Picture 30.jpg', 'Picture 31.jpg', 'Picture 32.jpg', 'Picture 33.jpg', 'Picture 34.jpg', 'Picture 35.jpg', 'Picture 36.jpg', 'Picture 37.jpg']
>>> print(natsort.natsorted(ll,reverse=True))
['Picture 37.jpg', 'Picture 36.jpg', 'Picture 35.jpg', 'Picture 34.jpg', 'Picture 33.jpg', 'Picture 32.jpg', 'Picture 31.jpg', 'Picture 30.jpg', 'Picture 29.jpg', 'Picture 28.jpg', 'Picture 27.jpg', 'Picture 26.jpg', 'Picture 25.jpg', 'Picture 24.jpg', 'Picture 23.jpg', 'Picture 22.jpg', 'Picture 21.jpg', 'Picture 20.jpg', 'Picture 19.jpg', 'Picture 18.jpg', 'Picture 17.jpg', 'Picture 16.jpg', 'Picture 15.jpg', 'Picture 14.jpg', 'Picture 13.jpg', 'Picture 12.jpg', 'Picture 11.jpg', 'Picture 10.jpg', 'Picture 3.jpg', 'Picture 2.jpg', 'Picture 1.jpg', 'Picture 0.jpg']
回答by hft
I have a directory with jpgs and other files in it.
我有一个包含 jpg 和其他文件的目录。
[...]
[...]
['0.jpg', '1.jpg', '10.jpg', '11.jpg', '12.jpg', '13.jpg', '14.jpg', '15.jpg', '16.jpg', '17.jpg', '18.jpg', '19.jpg', '2.jpg', '20.jpg', '21.jpg', '22.jpg', '23.jpg', '24.jpg', '25.jpg', '26.jpg', '27.jpg', '28.jpg', '29.jpg', '3.jpg', '30.jpg', '31.jpg', '32.jpg', '33.jpg', '34.jpg', '35.jpg', '36.jpg', '37.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg'] Clearly it sorts blindly the most significant number first. I tried using sorted() as you can see hoping that it would fix it but it makes no difference
['0.jpg', '1.jpg', '10.jpg', '11.jpg', '12.jpg', '13.jpg', '14.jpg', '15.jpg', ' 16.jpg'、'17.jpg'、'18.jpg'、'19.jpg'、'2.jpg'、'20.jpg'、'21.jpg'、'22.jpg'、'23。 jpg'、'24.jpg'、'25.jpg'、'26.jpg'、'27.jpg'、'28.jpg'、'29.jpg'、'3.jpg'、'30.jpg' , '31.jpg', '32.jpg', '33.jpg', '34.jpg', '35.jpg', '36.jpg', '37.jpg', '4.jpg', ' 5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg'] 显然它首先对最重要的数字进行盲目排序。我尝试使用 sorted(),因为你可以看到希望它会修复它,但它没有区别
You can use splitext to get the part without the extension and convert it to an int for the sorting. If the list is named 'l' and the sorted list is named 'lsorted' you can use:
您可以使用 splitext 获取没有扩展名的部分并将其转换为 int 进行排序。如果列表名为“l”且排序列表名为“lsorted”,则可以使用:
lsorted = sorted(l,key=lambda x: int(os.path.splitext(x)[0]))
l here is the list of images. If you have a directory of images, simply obtain a list of these images by :
我这里是图像列表。如果您有图像目录,只需通过以下方式获取这些图像的列表:
l = os.listdir('/path/to/directory/of/images')
Explanation: os.path.splitext on '10.jpg' returns ['10','.jpg'] so taking the int() of element zero will give you want you want as long as the filenames without the extention only contain strings that can be converted to integers with int(). Otherwise you will run into an Error.
解释: os.path.splitext on '10.jpg' 返回 ['10','.jpg'] 所以只要不带扩展名的文件名只包含字符串,取元素零的 int() 就会给你想要的可以使用 int() 转换为整数。否则你会遇到错误。