Instead of iterating over a file line by line, you want to iterate over a collection of fixed-sized records or chunks.
>>> from functools import partial
>>> SIZE = 32
>>> with open('somefile.data', 'rb') as f:
... records in iter(partial(f.read, SIZE), b'')
... for r in records:
... # do sth.
For text files, reading line by line is more common.
For binary files, read fixed-sized records.
>>> from functools import partial
>>> SIZE = 32
>>> with open('somefile.data', 'rb') as f:
... records in iter(partial(f.read, SIZE), b'')
... for r in records:
... # do sth.
For text files, reading line by line is more common.
For binary files, read fixed-sized records.
Comments
Post a Comment
https://gengwg.blogspot.com/