Python: for loop within a with not behaving as expected
<p>I am using the following code to combine two text files:</p>

<pre><code>def combine_acpd_ccs(self, ccs_file, acps_file, out_file):

    with open(ccs_file, 'r') as in_file1:
        with open(acps_file, 'r') as in_file2:
            with open(out_file, 'w') as out1:
                out1.write('PDB\tPA\tEHSS\tACPS\n')
                for line in in_file1:
                    segs = line.split()
                    for i in in_file2:
                        sse_score = i.split()
                        #print line
                        #print segs
                        if segs[0][:-4] == sse_score[0]:
                            out1.write(segs[0][:-4]+'\t'+segs[1]+'\t'+segs[2]+'\t'+sse_score[1]+'\n')
</code></pre>

<p>Example data looks like:</p>

<p>ccs_file:</p>

<pre><code>1b0o.pdb    1399.0  1772.0
1b8e.pdb    1397.0  1764.0
</code></pre>

<p>acps_file:</p>

<pre><code>1b0o    0.000756946316066
1b8e    8.40662008775
1b0o    6.25931529116
</code></pre>

<p>I expected my out put to be like:</p>

<pre><code>PDB PA  EHSS    ACPS
1b0o    1399.0  1772.0  0.000756946316066
1b0o    1399.0  1772.0  6.25931529116
1b8e    1397.0  1764.0 8.40662008775
</code></pre>

<p>But my codes just generates the top two lines of my expected output. If I print <code>segs</code> in the second <code>for</code> loop only the first line in <code>ccs_file</code> is passed to the loop. Any ideas where I have gone wrong?</p>
