On 6 9 ,   3 33 , HMS Surprise <[EMAIL PROTECTED]> wrote:
> Greetings,
>
> Could someone point my muddled head at a/the python repository. I know
> that one exists but cannot find it again. In particular I am looking
> for a standalone search tool that given a path searches files for a
> text string.
>
> Thanks,
>
> jvh

grep maybe the best choice.
if you want a pure python script
here it is.

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# File: grep.py
# Author: phus

try:
        import psyco
        psyco.full()
except ImportError:
        print 'no mod psyco'

import os, re

def grep_r(rx, path):
        root = path
        dirstack = [root]
        while len(dirstack) > 0:
                dir = dirstack.pop()
                try:
                        dirs = os.listdir(dir)
                except:
                        print "os.listdir('%s') error: %s" % (dir, os.error)
                        continue
                for name in dirs:
                        fullname = os.path.join(dir, name)
                        if os.path.isdir(fullname):
                                dirstack.append(fullname)
                        else:
                                grep(rx, fullname)

def grep(rx, file):
        try:
                f = open(file, "r")
                for line in f:
                        if rx.findall(line):
                                print file, ":", line.strip()
                f.close()
        except:
                print "grep error in %s" % file

if __name__ == "__main__":
        patern = "blah blah"
        rx = re.compile(patern, re.I)
        grep_r(rx, '.')

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to