Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Search for a string in binary files

Thread Tools

Re: Search for a string in binary files

John Hunter
Posts: n/a
>>>>> "hokieghal99" == hokieghal99 <(E-Mail Removed)> 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.


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary tree search vs Binary search Bogdan C Programming 22 10-21-2010 09:46 PM
Help understand probems - Binary Search and Sequenital Search Timmy C++ 5 07-09-2007 02:41 PM
Advantages of Binary Files over Text files in Search and read utab C++ 3 11-28-2006 03:09 PM
Binary Search to search linearizer table? Andy C Programming 1 11-25-2003 04:40 AM
Re: Search for a string in binary files =?iso-8859-1?q?Fran=E7ois_Pinard?= Python 2 07-22-2003 09:07 PM