'Python how to iterate over reading a binary file at an offset?

I'm trying to read a file starting at offset 0, then at offset 1, then at offset 2, etc. through the entire file. How would I achieve this? What would this code look like? I thought about using count from itertools, but I can't seem to figure it out.



Solution 1:[1]

Something simple like this should work:

with open('temp.bin', 'rb') as f:
    contents = f.read()

for i in range(len(contents)):
    print(contents[i:])

Instead of reading the file over and over again at offsets (using .seek()) it's better to load it into memory and just slice. Reading it at offsets loads it into memory either way, so it isn't more efficient and will take much longer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bharel