[Tutor] Search MS-Word

alan.gauld@bt.com alan.gauld@bt.com
Tue, 18 Jun 2002 11:03:26 +0100


> I need to search for strings in hundreds of .doc and
> .txt documents. I am in W98 with Python 2.1.1 -

doc files may pose a problem because they are in a proprietary 
binary format. The only satisfactory way might be to use COM 
to access the files via Wordpad or something.

However for basic text searching it's much easier.

For an exact match just use the string find() function

import string

string.find("Some string here", txtfile.read())

Or use the new string methods:

txtfile.read().find("Some string here")

If you need to pattern matcvh then you need the re module 
which has a search() function much like the string modules 
find() function except instead of a string you pass in a 
regular expression. My advice would be: get it working 
with string first then move to re later

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld