views:

62

answers:

1

Dear all,

I want to change the following code

for directory, dirs, files in os.walk(directory_1):
    do_something()

for directory, dirs, files in os.walk(directory_2):
    do_something()

to this code:

for directory, dirs, files in os.walk(directory_1) + os.walk(directory_2):
    do_something()

I get the error:

unsupported operand type(s) for +: 'generator' and 'generator'

My question is, how to join two generators in Python?

Thank you very much!

+10  A: 

I think itertools.chain() should do it.

Philipp
http://docs.python.org/library/itertools.html
reto
yep, this is exactly what `chain()` is for
gnibbler