Re: Search for a string in binary files

Discussion in 'Python' started by John Hunter, Jul 21, 2003.

  1. John Hunter

    John Hunter Guest

    >>>>> "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
    information.

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

    JDH
     
    John Hunter, Jul 21, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    1
    Views:
    367
    Jack Klein
    Nov 25, 2003
  2. =?iso-8859-1?q?Fran=E7ois_Pinard?=

    Re: Search for a string in binary files

    =?iso-8859-1?q?Fran=E7ois_Pinard?=, Jul 22, 2003, in forum: Python
    Replies:
    2
    Views:
    4,931
    =?iso-8859-1?q?Fran=E7ois_Pinard?=
    Jul 22, 2003
  3. utab
    Replies:
    3
    Views:
    871
  4. Timmy
    Replies:
    5
    Views:
    484
  5. Bogdan

    Binary tree search vs Binary search

    Bogdan, Oct 18, 2010, in forum: C Programming
    Replies:
    22
    Views:
    3,142
    Michael Angelo Ravera
    Oct 21, 2010
Loading...

Share This Page