将元素添加到Python生成 [英] Adding elements to python generators

查看:119
本文介绍了将元素添加到Python生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能追加要素巨蟒发电机?

Is it possible to append elements to a python generator?

我目前正在试图从一组杂乱无章的文件夹的获得所有图像,并将其写入到一个新的目录。要获得这些文件,我使用os.walk()返回在一个目录中的图像文件的列表。虽然我可以做一个发电机出这个单一的名单,我不知道如何将所有这些列表合并成一个单一的发电机。任何帮助将是非常美联社preciated。

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.

相关阅读:

  • Flattening a shallow list in python

推荐答案

这应该这样做,其中目录是目录列表:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆