Python Forum
search binary file and list all founded keyword offset - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: search binary file and list all founded keyword offset (/thread-25029.html)



search binary file and list all founded keyword offset - Pyguys - Mar-16-2020

search binary file and list all founded keyword offset

with open('1.bin', 'rb') as f:
        s = f.read()
    k = s.find(b'\xFF\x00')
    for list in k:
        print(list)
and i got error
TypeError:'int' object in not interable. 
please help. thanks.


RE: search binary file and list all founded keyword offset - scidam - Mar-16-2020

You can use regular expression for this, e.g.

import re

with open('1.bin', 'rb') as f:
    s = f.read()
    for match in s.finditer(b'\xFF\x00'): # UPD: should be re.finditer(b'\xFF\x00', s)
        print(match.span())



RE: search binary file and list all founded keyword offset - Pyguys - Mar-16-2020

(Mar-16-2020, 12:04 PM)scidam Wrote: You can use regular expression for this, e.g.

import re

with open('1.bin', 'rb') as f:
    s = f.read()
    for match in s.finditer(b'\xFF\x00'):
        print(match.span())

thanks for help.

i got error with your code. "AttributeError: 'bytes' object has no attribute 'finditer'"


and i try with
import re

with open('1.bin', 'rb') as f:
    s = f.read()
    for match in re.finditer(b'\xFF\x00'):
        print(match.span())
got error"TypeError: finditer() missing 1 required positional argument: 'string'"


RE: search binary file and list all founded keyword offset - scidam - Mar-16-2020

I'm sorry, that was a typo; It should be re.finditer(b'\xFF\x00', s).


RE: search binary file and list all founded keyword offset - Pyguys - Mar-17-2020

(Mar-16-2020, 11:41 PM)scidam Wrote: I'm sorry, that was a typo; It should be re.finditer(b'\xFF\x00', s).

thanks. it worked. Big Grin