Example: a grep script (1)

Find a string in a series of files:
grep.py 'Python' *.txt *.tmp
Python code:
def grep_file(string, filename):
    res = {}  # result: dict with key=line no. and value=line
    f = open(filename, 'r')
    line_no = 1
    for line in f:
        #if line.find(string) != -1:
        if re.search(string, line):
            res[line_no] = line
        line_no += 1

previousnexttable of contents