1#for small file:
2file = open("sample.bin", "rb")
3byte = file.read(1)
4while byte:
5 #do somthing with the byte
6 byte = file.read(1)
7file.close()
8
9#-------------------------------
10
11#for big file (biger the the memory):
12def bytes_from_file(filename, chunksize=8192): #8192 Byte = 8 kB (kib)
13 with open(filename, "rb") as f:
14 while True:
15 chunk = f.read(chunksize)
16 if chunk:
17 for b in chunk:
18 yield b
19 else:
20 break
21
22#call it
23for b in bytes_from_file('filename'):
24 do_stuff_with(b)