Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2007 Doug Hellmann.
  4. #
  5. """Writing to a memory mapped file with ACCESS_COPY.
  6.  
  7. """
  8.  
  9. __version__ = "$Id$"
  10. #end_pymotw_header
  11.  
  12. import mmap
  13. import shutil
  14. import contextlib
  15.  
  16. # Copy the example file
  17. shutil.copyfile('lorem.txt', 'lorem_copy.txt')
  18.  
  19. word = 'consectetuer'
  20. reversed = word[::-1]
  21.  
  22. with open('lorem_copy.txt', 'r+') as f:
  23. with contextlib.closing(mmap.mmap(f.fileno(), 0,
  24. access=mmap.ACCESS_COPY)
  25. ) as m:
  26. print 'Memory Before:'
  27. print m.readline().rstrip()
  28. print 'File Before :'
  29. print f.readline().rstrip()
  30. print
  31.  
  32. m.seek(0) # rewind
  33. loc = m.find(word)
  34. m[loc:loc+len(word)] = reversed
  35.  
  36. m.seek(0) # rewind
  37. print 'Memory After :'
  38. print m.readline().rstrip()
  39.  
  40. f.seek(0)
  41. print 'File After :'
  42. print f.readline().rstrip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement