Possible Duplicate:
How to join two generators in Python?
Is there a way in python to use os.walk to traverse multiple directories at once?
my_paths = []
path1 = '/path/to/directory/one/'
path2 = '/path/to/directory/two/'
for path, dirs, files in os.walk(path1, path2):
my_paths.append(dirs)
The above example doesn't work (as os.walk only accepts one directory), but I was hoping for a more elegant solution rather than calling os.walk twice (plus then I can sort it all at once). Thanks.
Use
itertools.chain()
.since nobody mentioned it, in this or the other referenced post:
http://docs.python.org/library/multiprocessing.html
in this case, you'd have a list of directories. the call to map would return a list of lists from each dir, you could then choose to flatten it, or keep your results clustered
Others have mentioned
itertools.chain
.There's also the option of just nesting one level more:
To treat multiples iterables as one, use
itertools.chain
: