Search for a string in binary files

François Pinard pinard at
Tue Jul 22 04:15:22 CEST 2003


> How could I use python to search for a string in binary files?  From the
> command line, I would do something like this on a Linux machine to find
> this string:

> grep -a "Microsoft Excel" *.xls

> How can I do this in Python?

Quite easily.  To get you started, here is an untested draft, I leave it to
you to try and debug. :-)

import glob
for name in glob.glob('*.xls'):
    if file(name, 'rb').read().find('Microsoft Excel') >= 0:
        print "Found in", name

François Pinard

More information about the Python-list mailing list