from string import ascii_lowercase
from random import choice
from io import StringIO
buf = ''.join(choice(ascii_lowercase) for _ in range(50))
print(buf)
sio_buf = StringIO(buf)
first16 = sio_buf.read(16)
print(first16)
from collections import deque
last16 = deque(iter(lambda : sio_buf.read(1), ''), 16)
print(''.join(last16))
Is there a Python module for transparently working with a file-like object as a buffer?