[Tutor] Reading & printing lines from two different files

stuart_clemons@us.ibm.com stuart_clemons@us.ibm.com
Thu, 6 Jun 2002 13:57:02 -0400


--0__=0ABBE143DFCC294C8f9e8a93df938690918c0ABBE143DFCC294C
Content-type: text/plain; charset=US-ASCII


Hi all:

I have two files, file1 and file2.  I want to print the first line from
file1, then the first line from file2, then the second line from file 1,
then the second line from file 2, etc.

Here are my files and what the output should look like:

File1.txt
First line file 1
Second line file 1
Third line file 1

File2.txt
First line file 2
Second line file 2
Third line file 3

This is the output I want when the program is run:

First line file 1
First line file 2
Second line file 1
Second line file 2
Third line file 1
Third line file 2

I've tried different looping techniques mainly using variations of the
structure below, but I can't seem to get the output I want.  Any
suggestions ?  Thanks.

infile1 = open('c:\file1.txt', 'r')
infile2 = open ('c:\file2.txt', 'r')
      for line1 in infile1.readlines():
            print line1
      for line2 in infile2.readlines()
            print line2
--0__=0ABBE143DFCC294C8f9e8a93df938690918c0ABBE143DFCC294C
Content-type: text/html; charset=US-ASCII
Content-Disposition: inline

<html><body>
<p>Hi all:<br>
<br>
I have two files, file1 and file2.  I want to print the first line from file1, then the first line from file2, then the second line from file 1, then the second line from file 2, etc.<br>
<br>
Here are my files and what the output should look like:<br>
<br>
File1.txt<br>
First line file 1<br>
Second line file 1<br>
Third line file 1<br>
<br>
File2.txt<br>
First line file 2<br>
Second line file 2<br>
Third line file 3<br>
<br>
This is the output I want when the program is run:<br>
<br>
First line file 1<br>
First line file 2<br>
Second line file 1<br>
Second line file 2<br>
Third line file 1<br>
Third line file 2<br>
<br>
I've tried different looping techniques mainly using variations of the structure below, but I can't seem to get the output I want.  Any suggestions ?  Thanks.<br>
<br>
infile1 = open('c:\file1.txt', 'r')<br>
infile2 = open ('c:\file2.txt', 'r')<br>
	for line1 in infile1.readlines():<br>
		print line1<br>
	for line2 in infile2.readlines()<br>
		print line2<br>
</body></html>
--0__=0ABBE143DFCC294C8f9e8a93df938690918c0ABBE143DFCC294C--