Reputation: 47
I have opened a picture file and saved on a variable.
file_input = open(FILE_PATH, 'rb')
file_doc = file_input.read()
How can I split the variable to a list by size(each part of the list is the size of 1024 bytes)?
Upvotes: 1
Views: 1982
Reputation: 7562
You can split a bytes object just by indexing the corresponding slice, e.g.
>>> x = b'aabbcc'
>>> [x[i:i+2] for i in range(0,len(x)-1,2)]
[b'aa', b'bb', b'cc']
But in general, I'd agree with @COLDSPEED: just read the file in chunks of 1024 bytes and do whatever you need with each chunk:
with open(FILE_PATH, "rb") as f:
while True:
data = f.read(1024)
if not data: break
process_1k_bytes(data)
Upvotes: 3