0

I'm using Python's bytearrays to read from files using the readinto function from BufferedIOBase. This function requires a bytes-like object. I would like to have a header in the first few positions of this bytearray but the rest of the bytearray should be read from the file. Essentially I need to readinto buffer[n:] where n is the number of bytes in my header. However, If I pass in a list slice of buffer[n:], data is read into this list slice but modifying the slice of a list does not modify the original, so effectively I get no data. In C this is very simple with pointers, but to my knowledge there are no pointers in Python?

Furthermore, My project is quite memory limited, so I am only able to use 1 bytearray buffer for most data being read in or written out. List slicing creating this copy can sometimes cause a memory error where the device is unable to allocate enough memory for the slice. This is why I cannot use the read() function as it creates a new bytearray which will likely create memory errors as well.

This is the current behavior of list slicing:

>>> mylist = [0] * 5
>>> mylist_sliced = mylist[1:]
>>> mylist_sliced[3] = 5
>>> print(mylist, mylist_sliced)
[0, 0, 0, 0, 0] [0, 0, 0, 5]

What I need is something like this:

>>> mylist = [0] * 5
>>> mylist_sliced = mylist[1:]
>>> mylist_sliced[3] = 5
>>> print(mylist, mylist_sliced)
[0, 0, 0, 0, 5] [0, 0, 0, 5]

Modifying the slice modifies the original. I haven't been able to find in the Python documentation for bytes-like objects or list objects a way to do this.

  • 1
    Can you use [`memoryview`](https://docs.python.org/dev/library/stdtypes.html#memoryview)? – Barmar Jul 10 '23 at 16:30

0 Answers0