Use grep on file in Python
<p>I have searched the grep answers on here and cannot find an answer.  They all seem to search for a string in a file, not a <strong>list of strings</strong> from a file.  I already have a search function that works, but grep does it WAY faster.  I have a list of strings in a file sn.txt (with one string on each line, no deliminators).  I want to search another file (Merge_EXP.exp) for lines that have a match and write it out to a new file.  The file I am searching in has a half millions lines, so searching for a few thousand in there takes hours without grep.</p>

<p>When I run it from command prompt in windows, it does it in minutes:</p>

<pre><code>grep --file=sn.txt Merge_EXP.exp &gt; Merge_EXP_Out.exp
</code></pre>

<p>How can I call this same process from Python?  I don't really want alternatives in Python because I already have one that works but takes a while.  Unless you think you can significantly improve the performance of that:</p>

<pre><code>def match_SN(serialnumb, Exp_Merge, output_exp):
    fout = open(output_exp,'a')
    f = open(Exp_Merge,'r')
    # skip first line
    f.readline()
    for record in f:
        record = record.strip().rstrip('\n')
        if serialnumb in record:
            fout.write (record + '\n')
    f.close()
    fout.close()

def main(Output_CSV, Exp_Merge, updated_exp):

    # create a blank output
    fout = open(updated_exp,'w')

    # copy header records
    f = open(Exp_Merge,'r')
    header1 = f.readline()
    fout.write(header1)
    header2 = f.readline()
    fout.write(header2)
    fout.close()
    f.close()

    f_csv = open(Output_CSV,'r')
    f_csv.readline()
    for rec in f_csv:
        rec_list = rec.split(",")
        sn = rec_list[2]
        sn = sn.strip().rstrip('\n')
        match_SN(sn,Exp_Merge,updated_exp)
</code></pre>
