0

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?

Eric
  • 95,302
  • 53
  • 242
  • 374
  • Could you get the size first as in https://stackoverflow.com/questions/6591931/getting-file-size-in-python – Simon Crane May 22 '20 at 18:20
  • 1
    you need size before allocating a big enough `bytearray`. If the bytearray is too large, `memoryview(data)[:n_read]` may help with the expense of the unused allocated memory. – Aaron May 22 '20 at 18:20
  • I had forgotten that `memoryview` behaved similarly enough to `bytearray` that the difference doesn't really matter, thanks @Aaron – Eric May 22 '20 at 18:21
  • For what it's worth, the CPython implementation of `open(...).readall()` ends up [here](https://github.com/python/cpython/blob/905b3cd05f8d2c29e1605d109900e3e9d07af4d3/Modules/_io/fileio.c#L682-L770) – Eric May 24 '20 at 13:34

0 Answers0