
python.list at tim
Nov 5, 2009, 8:06 AM
Post #3 of 3
(43 views)
Permalink
|
> I want to be able to take two or more open read handles and > concatenate them into an object that behaves like a regular read > handle (i.e. a file object open for reading), but behind the scenes > it reads from the concatenated handles in sequence. I.e. I want > the read-handle equivalent of the standard Unix utility cat. (The > reason I can't use subprocess and cat for this is that I want to > concatenate read handles that do not necessarily come from files.) Sounds like itertools.chain would do what you want: for line in itertools.chain( file('a.txt'), file('b.txt'), ): do_something(line) or more generically if you have a list of file objects: lst = [file('a.txt'), file('b.txt'), ...] for line in itertools.chain(*lst): do_something(line) -tkc -- http://mail.python.org/mailman/listinfo/python-list
|