Guest User

Untitled

a guest
Jan 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import bz2
  2. import mmap
  3.  
  4. lines = '''This is my first line
  5. This is the second
  6. And the third
  7. '''
  8.  
  9. with open("bz2TestFile", "wb") as f:
  10. f.write(bz2.compress(lines))
  11.  
  12. with open("bz2TestFile", "rb") as f:
  13. mapped = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
  14.  
  15. print "Part of MMAPPED"
  16. # This does not work until I hit a minimum length
  17. # due to (I believe) the checksums in the bz2 algorithm
  18. #
  19. for x in range(len(mapped)+2):
  20. line = mapped[0:x]
  21. try:
  22. print x
  23. print bz2.decompress(line)
  24. except:
  25. pass
  26.  
  27. # I can decompress the entire mmapped file
  28. print ":entire mmap file:"
  29. print bz2.decompress(mapped)
  30.  
  31. # I can create a bz2File object from the file path
  32. # Is there a way to map the mmap object to this function?
  33. print ":BZ2 File readline:"
  34. bzF = bz2.BZ2File("bz2TestFile")
  35.  
  36. # Seek to specific offset
  37. bzF.seek(22)
  38. # Read the data
  39. print bzF.readline()
Add Comment
Please, Sign In to add comment