In Python, how do I iterate over one iterator and then another?

I would like to repeat two different iterators, something like this:

file1 = open('file1', 'r') file2 = open('file2', 'r') for item in one_then_another(file1, file2): print item 

What would I expect to print all lines of file1, and then all lines of file2.

I need something in common, since iterators may not be files, this is just an example. I know I can do this with:

 for item in [file1]+[file2]: 

but it reads both files into memory, which I would rather avoid.

+48
python file iteration
Feb 17 '14 at 10:04
source share
3 answers

Use itertools.chain :

 from itertools import chain for line in chain(file1, file2): pass 

fileinput module also provides a similar function:

 import fileinput for line in fileinput.input(['file1', 'file2']): pass 
+88
Feb 17 '14 at 10:05
source share
β€” -

You can also do this with a simple expression :

 for line in (l for f in (file1, file2) for l in f): # do something with line 

using this method, you can specify some condition in the expression itself:

 for line in (l for f in (file1, file2) for l in f if 'text' in l): # do something with line which contains 'text' 

The above example is equivalent to this generator with cycle:

 def genlinewithtext(*files): for file in files: for line in file: if 'text' in line: yield line for line in genlinewithtext(file1, file2): # do something with line which contains 'text' 
+17
Feb 17 '14 at 10:24
source share

I think the most Pythonic approach to this particular file problem is to use the fileinput module (since you either need complex context managers or error handling with open ), I'm going to start with the Ashwini example, but add a few things. Firstly, it’s better to open the U flag to support Universal Newlines (assuming your Python is compiled with it, and most of them are), ( r is the default mode, but explicit is better than implicit). If you work with other people, it is best to support them by giving you files in any format.

 import fileinput for line in fileinput.input(['file1', 'file2'], mode='rU'): pass 

This can also be used on the command line, as it will accept sys.argv [1:] if you do this:

 import fileinput for line in fileinput.input(mode='rU'): pass 

And you will transfer the files in your shell as follows:

 $ python myscript.py file1 file2 
+7
Feb 17 '14 at 19:01
source share



All Articles