Search for a string in binary files

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon Jul 21 18:06:25 EDT 2003


>>>>> "hokieghal99" == hokieghal99  <hokiegal99 at hotmail.com> writes:

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

    hokieghal99> grep -a "Microsoft Excel" *.xls

    hokieghal99> How can I do this in Python?

Linux doesn't distinguish between binary files and regular files, so
you can use the usual string find method.  On windows, you need to be
sure to open the file in binary mode


  >>> s = file('temp.xls', 'rb').read()
  >>> s.find('Excel')

The point is that there is nothing special about searching binary
files.  Search the google groups archives for python grep for more
information.

You can use regular expressions if you want to do more sophisticated
pattern matching.

JDH





More information about the Python-list mailing list