0

I have a binary file with some text on it. To search for it I'd write, in good old bash:

grep -a 'pattern' binaryfile

I'd like to achieve the same in Python using the re module. The reason is that I want my script to work on multiple platforms.

After reading a bit around I wrote a small grep function, and it looks like this:

import re
def grep(pattern, binaryfile):
    regexobj = re.compile(pattern)
    for line in binaryfile:
        if regexobj.search(line):
                yield line

The code works fine on normal text files.

with open('pythoncode.py','r') as f:
    print(list(grep('import', f)))

But if I try it on a binary file with open(binaryfile, 'rb'), then it throws the following error:

TypeError: cannot use a string pattern on a bytes-like object

Before I start to dig into all sorts of ad-hoc solutions to my problem... Is there a simple Pythonic way to use the re module to search for patterns in binary files?

Daniel
  • 11,332
  • 9
  • 44
  • 72

0 Answers0