Iterate across lines in two files in sequence
python
Solution
As has been pointed out `itertools.chain` is an option, however there's also another useful standard module which avoids having to explicitly use `open`...
import fileinput
for line in fileinput.input(['file1.txt', 'file2.txt']):
print line
This also has some handy functions for line number and filename etc... Check the docs at http://docs.python.org/library/fileinput.html
in reply to comments - using with a context manager
from contextlib import closing
with closing(fileinput.input(['file1.txt', 'file2.txt'])) as infiles:
for line in infiles:
pass # stuff
Problem
I have two files, and I want to perform some line-wise operation across both of them (one by one). I am now using two loops to achieve this. Is there a way to do it in a single loop (in python 2.7): ``` for fileName in [fileNam1,fileName2]: for line in open(fileName): do something ```