Read lines from two different files and print line number if match is found [edited]
<p>I have two files. I want to read each line from file1 and check if it matches with any line in file2. I want this to repeat for each line in file1, and print the line number of file1 whose match was found in file2. So far I have this. It works for test files of 4-5 lines each but when working on large files of over 60k lines, I am getting a blank output</p>

<pre><code>    num=0
    f1 = open('pdataf.txt', 'r')
    f2 = open('splitc.txt', 'r')
    fo = open('op.txt', 'w')
    for line1 in f1:
        for line2 in f2:
            num=num+1
            if line1==line2:
                nummy=str(num)
                fo.write(nummy)
                fo.write('\n')
                break
        continue
    f1.close()
    f2.close()
</code></pre>

<p>Firstly, you have a syntax error on line 8, replace the line with <code>print(num)</code>.
I don't have a lot of information about your problem, it might be a good idea to clarify, but what I suspect happens is that you have an end line character <code>"\n"</code> at the end of the lines you read.</p>

<p>To get rid of this character, you can use the <code>rstrip()</code> method as discussed in <a href="http://stackoverflow.com/questions/275018/how-can-i-remove-chomp-a-newline-in-python">this topic</a>.</p>

<p>So, I would suggest replacing your code by :</p>

<pre><code>num=0
f1 = open('s.txt', 'r')
f2 = open('p.txt', 'r')
for line1 in f1:
    line1 = line1.rstrip()
    for line2 in f2:
        line2 = line2.rstrip()
        if line1==line2:
            num=num+1
            print(num)
f1.close()
f2.close()
</code></pre>

python python-2.7 text