Python Forum
search binary file and list all founded keyword offset
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search binary file and list all founded keyword offset
#1
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.
Reply
#2
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())
Reply
#3
(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'"
Reply
#4
I'm sorry, that was a typo; It should be re.finditer(b'\xFF\x00', s).
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic binary search algorithm - using a while loop Drone4four 1 361 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  Why doesn't list require global keyword? johnywhy 9 811 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Search Excel File with a list of values huzzug 4 1,242 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Find a specific keyword after another keyword and change the output sgtmcc 5 817 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,331 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  How do I read and write a binary file in Python? blackears 6 6,597 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  search file by regex SamLiu 1 913 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  If function is false search next file mattbatt84 2 1,151 Sep-04-2022, 01:56 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,565 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  search a list or tuple for a specific type ot class Skaperen 8 1,926 Jul-22-2022, 10:29 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020