[Tutor] read file and match string

Jim Ragsdale overlord@netdoor.com
Thu, 31 Jan 2002 13:11:40 -0600


Warning: Novice python programmer :)
I was suggested this snippet of code to  read a possible long file and do a
string match. it is supposed to be faster.

import mmap, re
def search(filename, rx):
    f = open(filename, 'r+')
    mem = mmap.mmap(f.fileno(), 0)
    for match in rx.finditer(mem):
        print match.group(0)
    mem.close()
    f.close()

This is what i am running now.

import sys, re, os, time

p = re.compile('PLAYER_JOIN')
ReadFile = sys.argv[1]
WriteFile = sys.argv[2]
rf = open(ReadFile, 'r')
wf = open(WriteFile, 'w')
 for line in rf.xreadlines():
        m = p.search(line)
        if m:
            wf.writelines(line)
wf.close
rf.close

Can someone explain the top snippet to me? looks like a function that takes
a filename argument and what is the rx? What is the string to match the file
against? Is that the rx arg? Is this what is needed for what i am doing or
is it slightly different? The mem line looks like it opens the file like
xreadlines.

Any help would be appreciated. Thanks!


Jim Ragsdale