This doesn't actually fix the problem but is a work around. Instead of:
data = np.fromfile(filepath,dtype=np.uint8)
I replaced it with:
with open(filepath, 'rb') as file:
data = array.array('B',file.read())
data = np.array(data)
Now I can run the script without without error and without using F9.