Velocity Reviews

Velocity Reviews (
-   Python (
-   -   Re: Search for a string in binary files (

John Hunter 07-21-2003 10:06 PM

Re: Search for a string in binary files
>>>>> "hokieghal99" == hokieghal99 <> 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

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


All times are GMT. The time now is 01:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.