1

I want to open a binary file (yes, another soft synth soundbank) in ASCII format and check if it contains a string or not. There are multiple files in the folder, but I have written the appropriate code for it, I just want it to search the file for a substring.

I've tried opening the same format using the ASCII encoding function before, but it does not display the data I want (it displays some garbled data, totally different from what it does in a hex editor, in which the file is opened in ASCII). Can someone point me in the right direction?

EDIT: As asked below, here is the new code I'm using:

# sbf_check.py (sample code I've written to test the sbf file before implementing in into the main.py file)

path = "C:\\Users\\User\\AppData\\Roaming\\RevealSound\\Banks\\Aura Qualic Trance.sbf"
file = open(path, "rb")

for x in file:
    line = file.readline()
    new = line.decode("ASCII")
    print(new)

main.py file:

import glob, os

path = "C:\\Users\\User\\AppData\\Roaming\\RevealSound\\Banks"

for filename in glob.glob(os.path.join(path, "*.sbf")):
    with open(os.path.join(os.getcwd(), filename), "r") as f:
        # code to decode sbf file to ASCII, then search for the substring in the main string

Hex editor: Image 1

Image 2

(Note: the data circled with red does not matter to me as it's parameter data, I just want to search for the preset name. It's not like my previous question, where I needed to skip the parameter data.)

Code output (VS Code):

Traceback (most recent call last):
  File "c:\Users\User\Desktop\Programming\sbf_check.py", line 6, in <module>
    new = line.decode("ASCII")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 0: ordinal not in range(128)
7
  • 1
    Could you give a concrete example? Commented Apr 24, 2021 at 21:52
  • @xjcl Of the file I want to open? It's a Spire soundbank (.sbf). I don't care about the parameter data, I just want to check if a substring (preset name) is there. I have asked a similar question about Sylenth1 soundbanks before, as a result I did not write much details to avoid it being flagged as a duplicate to my previous question. Commented Apr 24, 2021 at 21:53
  • 1
    I mean can you show an example file in hexedit and what your code is outputting? They should really match up. Commented Apr 24, 2021 at 21:55
  • @xjcl Oh, sure. I'll edit it in a bit. Commented Apr 24, 2021 at 21:56
  • @xjcl It's probably fine now, can you check it? Commented Apr 24, 2021 at 22:09

1 Answer 1

2

Does the following do what you want? It should handle non-UTF-8 characters by displaying a tofu box instead of throwing a decoding error.

path = "C:\\Users\\User\\AppData\\Roaming\\RevealSound\\Banks\\" \
       "Aura Qualic Trance.sbf"

with open(path, errors='ignore') as f:
    print(f.read())
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it does! Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.