列出文件夹中的png文件
我正在尝试在包含其他文件夹的文件夹中列出所有png文件的列表.这是我的代码.
I'm trying to make a list of all png files in a folder that contains other folders. Here's my code.
import os
filelist=os.listdir('images')
for fichier in filelist:
if not(fichier.endswith(".png")):
filelist.remove(fichier)
print(filelist)
问题是,最后一张照片显示某些子文件夹(但不是全部)逃脱了剔除...
problem is, that last print shows that some of the subfolders (but not all) escaped the culling...
['aa.png', 'Nouveau dossier - Copie', 'Nouveau dossier - Copie (3)', 'Nouveau dossier - Copie (5)', 'zz.png']
我在做什么错了?
摘要:永远,永远,永远修改要迭代的列表.
Summary: Never, ever, ever modify the list that you are iterating over.
相反,遍历一个副本:
import os
filelist=os.listdir('images')
for fichier in filelist[:]: # filelist[:] makes a copy of filelist.
if not(fichier.endswith(".png")):
filelist.remove(fichier)
print(filelist)
或者,如果您不希望制作不必要的副本,请反向进行迭代(仅当您可以保证列表中的项目是唯一的时才有效;对于文件系统,这是一个很好的假设):
Or if you don't like to make unnecessary copies, iterate in reverse (this will only work if you can guarantee that the items in the list are unique; for a file system this is a good assumption):
for fichier in reversed(filelist):
# do stuff
请注意,您可以使用Python的glob
模块来简化此操作:
Note that you can use Python's glob
module to simplify this:
import glob
print(glob.glob('images/*.png'))
原因
当您遍历Python中的列表时,在后台,Python实际上正在遍历该列表的索引.您可以看到,每当您实际删除这些项目时,这都是一个巨大的问题:
The reason why
When you iterate over a list in Python, behind the scenes Python is actually iterating over the indices of the list. You can see that this is a huge problem whenever you actually remove the items:
l = [1, 2, 2, 3]:
for x in l:
if x%2 == 0:
l.remove(x)
print('x == {}'.format(x))
print('l == {}'.format(l))
您可以通过此处显示的内容来跳过第二个2,并且l
的值为[1, 2, 3]
.这是因为,每当到达和删除前两个2时,索引都是1
(第二个元素).在下一次迭代中,索引为2
.此时为l == [1,2,3]
,因此为x == 3
.如果您运行代码,则可能比该解释更为明显.
You can tell by what is printed here that the second 2 is skipped, and that l
has a value of [1, 2, 3]
. This is because, whenever the first 2 is reached and removed, the index is 1
(the second element). On the next iteration, the index is 2
. At this point, l == [1,2,3]
, so x == 3
. If you run the code, it will probably be more obvious than this explanation.