d = []
with open(file, 'rb') as f:
for i in f.readlines():
print(i)
d.append(i)
#
d = []
with open(file, 'r') as f:
for i in f:
print(i)
d.append(i)
byte_count = 8
with open("myfile", "rb") as f:
byte = f.read(byte_count)
while byte != b"":
# Do stuff with byte.
byte = f.read(byte_count)
>>> def read_blocks(fname, block_size):
... with open(fname, 'rb') as fin:
... while True:
... data = fin.read(block_size)
... if data:
... yield data
... else:
... break
...
>>> out = read_blocks('/etc/passwd', 8)
>>>
>>> list(out)[:5]
[b'root:x:0', b':0:root:', b'/root:/b', b'in/bash\n', b'bin:x:1:']
>>>
>>> out = read_blocks('/etc/passwd', 10)
>>>
>>> list(out)[:5]
[b'root:x:0:0', b':root:/roo', b't:/bin/bas', b'h\nbin:x:1:', b'1:bin:/bin']
>>>