I can easily read a file into a bytearray
using:
with open('file.dat', 'rb') as f:
data_readonly : bytes = f.read()
data = bytearray(data_readonly)
However, the call to bytearray
performs a copy.
If I know the size of the file in advance, I can use:
SIZE = ...
data = bytearray(SIZE)
with open('file.dat', 'rb') as f:
n_read = f.readinto(b)
assert n_read == SIZE
# data[:n_read] would perform a copy, which is what I was trying not to do!
What can I do if I don't know the size of the file in advance?