[Tutor] Searching across .Py files for a particular string/character

David Heiser David.Heiser at intelliden.com
Fri Mar 31 04:17:39 CEST 2006


Here's a simple Python script that will do it. It's not very
sophisticated, but it's easy to modify for special cases.

import os, string

def Find(TargetString, DIR, Names):
    for Name in Names:
        if Name != "Search.py":
            try:
                TargetFile = DIR + "/" + Name
                Blob = open(TargetFile, "r").read()
                if Blob.find(TargetString) > -1:
                    print TargetFile
            except IOError:
                pass
    return

TargetString = 'telnetlib'

print "\nFinding " + TargetString + "\n---------------\n"
os.path.walk(".", Find, TargetString)
==============================================



-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Janesh Ramakrishnan
Sent: Thursday, March 30, 2006 5:57 PM
To: tutor at python.org
Subject: [Tutor] Searching across .Py files for a particular
string/character



Hi Folks,

I was wondering what would be the best way to look up a string across
different files in the Python interpreter (PythonWin 2.4). The find
function only finds files within currently open files. If I have a
folder of .py scripts and need to look up a specific keyword or string
among all these files within the project folder, is there any method
that you'd recommend?

For eg: Visual Studio 6.0 can look for a string across numerous files
indexed in a project and returns a list of files containing that
specific string. 

Any help would be greatly appreciated.

Thanks.
Janesh



_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list