Skip to content

Instantly share code, notes, and snippets.

@gdlmx
Created March 20, 2023 16:36
Show Gist options
  • Save gdlmx/5056eaf91e77d4d2c9895c4e15e9984e to your computer and use it in GitHub Desktop.
Save gdlmx/5056eaf91e77d4d2c9895c4e15e9984e to your computer and use it in GitHub Desktop.
Create memory view of file larger than RAM
import mmap
import zlib
import sys
from contextlib import contextmanager
@contextmanager
def f2bytes(fname):
with open(fname, 'rb') as f :
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as m:
mmview = memoryview(m)
try:
yield mmview
finally:
mmview.release()
def main(fname):
with f2bytes(fname) as m:
return zlib.crc32(m)
if __name__ == '__main__':
n = sys.argv[1]
print(n,'crc32 =', hex(main(n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment