Python Forum
Split Bytearray into separate Files by Hex delimter - 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: Split Bytearray into separate Files by Hex delimter (/thread-39560.html)



Split Bytearray into separate Files by Hex delimter - lastyle - Mar-08-2023

Hi all

i am loading a file into a byte aray with

f = open("myfile.seq", 'rb')
while True:
binarycontent = f.read(-1)
if not binarycontent:
break
print(binarycontent)

after that has been loaded i like to split that array by hex value $93 and save the splits to disk.
Searching the forum just lead me to solutions with fixed lenghts, how should i split that array into parts ?


RE: Split Bytearray into separate Files by Hex delimter - bowlofred - Mar-08-2023

Bytearray has a split() method, so you can separate by a hex value.

>>> a = bytearray(b'hello\x93there\x93'*4)
>>> a
bytearray(b'hello\x93there\x93hello\x93there\x93hello\x93there\x93hello\x93there\x93')
>>> a.split(b'\x93')
[bytearray(b'hello'), bytearray(b'there'), bytearray(b'hello'), bytearray(b'there'), bytearray(b'hello'), bytearray(b'there'), bytearray(b'hello'), bytearray(b'there'), bytearray(b'')]



RE: Split Bytearray into separate Files by Hex delimter - deanhystad - Mar-08-2023

Splitting the array into parts is easy (use split()). But how do you save the chunks? if you write the chunks to a binary file how do you delimit the chunks? It is not like a text file where there is a default delimiter ('\n'). Do you plan to write each chunk to a different file? If you write them all back to the same file, using a delimiter to separate, what is the advantage over not splitting up the file in the first place?

Just re-read the title. Writing chunks to different files. Nevermind.


RE: Split Bytearray into separate Files by Hex delimter - lastyle - Mar-08-2023

(Mar-08-2023, 09:15 PM)deanhystad Wrote: Splitting the array into parts is easy (use split()). But how do you save the chunks? if you write the chunks to a binary file how do you delimit the chunks? It is not like a text file where there is a default delimiter ('\n'). Do you plan to write each chunk to a different file? If you write them all back to the same file, using a delimiter to separate, what is the advantage over not splitting up the file in the first place?

I dont want to write them Back to the Same File. Goal ist to have multiple Files ofcourse. If there is a way to Split then without loading into Array that would work too.


RE: Split Bytearray into separate Files by Hex delimter - lastyle - Mar-09-2023

What i still dont understand is how do i save the split contents to files with ascending filenames. Somehow i always save the whole array instad of the split chunks


RE: Split Bytearray into separate Files by Hex delimter - bowlofred - Mar-09-2023

(Mar-09-2023, 12:30 AM)lastyle Wrote: What i still dont understand is how do i save the split contents to files with ascending filenames. Somehow i always save the whole array instad of the split chunks

Loop over the output of the split.
Within the loop open a new writable file.
Write the data to the file.

Give it a try and show us the code you've got so far.