Search for a string in binary files

François Pinard pinard at iro.umontreal.ca
Mon Jul 21 22:15:22 EDT 2003


[hokieghal99]

> 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   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list