Reading two text files line by line simultaneously

    with open("textfile1") as textfile1, open("textfile2") as textfile2: 
        for x, y in izip(textfile1, textfile2):
            x = x.strip()
            y = y.strip()
            print(f"{x}\t{y}")

In Python 2, replace built-in zip with itertools.izip:

    from itertools import izip

    with open("textfile1") as textfile1, open("textfile2") as textfile2: 
        for x, y in izip(textfile1, textfile2):
            x = x.strip()
            y = y.strip()
            print("{0}\t{1}".format(x, y))

Leave a Comment