w3resource logo


Python Exercises

Python File I/O: Combine each line from first file with the corresponding line in second file

Python File I/O: Exercise-14 with Solution

Write a Python program to combine each line from first file with the corresponding line in second file.

Sample Solution :-

Python Code :

with open('abc.txt') as fh1, open('test.txt') as fh2:
    for line1, line2 in zip(fh1, fh2):
        # line1 from abc.txt, line2 from test.txtg
        print(line1+line2)

Output :

Red
lasith madushanka

Python Code Editor:

with open('abc.txt') as fh1, open('test.txt') as fh2:
    for line1, line2 in zip(fh1, fh2):
        # line1 from abc.txt, line2 from test.txtg
        print(line1+line2)

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number