Read blocks from a file object until x bytes from the end
问题 I need to read chunks of 64KB in loop, and process them, but stop at the end of file minus 16 bytes : the last 16 bytes are a tag metadata. The file might be super large, so I can't read it all in RAM. All the solutions I find are a bit clumsy and/or unpythonic. with open('myfile', 'rb') as f: while True: block = f.read(65536) if not block: break process_block(block) If 16 <= len(block) < 65536 , it's easy: it's the last block ever. So useful_data = block[:-16] and tag = block[-16:] If len