将元素添加到 python 生成器 [英] Adding elements to python generators
本文介绍了将元素添加到 python 生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将元素附加到 python 生成器?
Is it possible to append elements to a python generator?
我目前正在尝试从一组杂乱无章的文件夹中获取所有图像并将它们写入一个新目录.为了获取文件,我使用 os.walk() 返回单个目录中的图像文件列表.虽然我可以用这个单一的列表制作一个生成器,但我不知道如何将所有这些列表组合成一个单一的生成器.任何帮助将不胜感激.
I'm currently trying to get all images from a set of disorganized folders and write them to a new directory. To get the files, I'm using os.walk() which returns a list of image files in a single directory. While I can make a generator out of this single list, I don't know how to combine all these lists into one single generator. Any help would be much appreciated.
相关:
推荐答案
应该这样做,其中 directories
是您的目录列表:
This should do it, where directories
is your list of directories:
import os
import itertools
generators = [os.walk(d) for d in directories]
for root, dirs, files in itertools.chain(*generators):
print root, dirs, files
这篇关于将元素添加到 python 生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文