#!/usr/bin/python3
# ===================================================================
# read "chunks" (in bytes) from a file
# -------------------------------------------------------------------
#
# different code found on the web - read nbytes from a file
# (may or may not read to the end of the file)
#
# def read_bytes(filename,nbytes):
# with open(filename.'rb':
# while True:
# byt = file.read(1)
# if byt:
# yield byt
# else:
# break
# if nbytes > 0:
# nbytes -= 1
# if nbytes == 0:
# break
#
# ===================================================================
# -------------------------------------------------------------------
# ---- read/return nb bytes from fin
# -------------------------------------------------------------------
def read_bytes(fin,nb):
b = fin.read(nb)
return b
# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------
if __name__ == '__main__':
filename = './Pictures/iss.png'
loopcount = 4
with open(filename,'rb') as fin:
while True:
# ---- for testing, limit the number of read loops
if loopcount <= 0:
break
loopcount -= 1
# ---- read 4 bytes from the file
byt = read_bytes(fin,4)
print(f'bytes read {len(byt)}')
# ---- end of file
if len(byt) == 0:
break
# ---- what did we read
print(f'type: {type(byt)} bytes: {byt}')