29

Is it possible to append elements to a python generator?

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.

Related:

Community
  • 1
  • 1
Francisco
  • 375
  • 2
  • 5
  • 9

4 Answers4

31

You are looking for itertools.chain. It will combine multiple iterables into a single one, like this:

>>> import itertools 
>>> for i in itertools.chain([1,2,3], [4,5,6]):
...     print(i)
... 
1
2
3
4
5
6
Stefan
  • 1,697
  • 15
  • 31
dF.
  • 74,139
  • 30
  • 130
  • 136
19

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
Ryan Bright
  • 3,495
  • 21
  • 20
4
def files_gen(topdir='.'):
    for root, dirs, files in os.walk(topdir):
        # ... do some stuff with files
        for f in files:
            yield os.path.join(root, f)
        # ... do other stuff

for f in files_gen():
    print f
jfs
  • 399,953
  • 195
  • 994
  • 1,670
-1

Like this.

def threeGens( i, j, k ):
    for x in range(i):
       yield x
    for x in range(j):
       yield x
    for x in range(k):
       yield x

Works well.

S.Lott
  • 384,516
  • 81
  • 508
  • 779