Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1/usr/bin/env python
- # this is from The Python
- # Standard Library by example
- # ISBN13: 9780321767349
- import mmap
- import shutil
- import contextlib
- # Copy the example file
- shutil.copyfile('lorem.txt', 'lorem_copy.txt')
- word = 'consectetuer'
- reversed = word[::-1]
- with open('lorem_copy.text', 'r+') as f:
- with contextlib.closing(mmap.mmap(f.fileno(), 0,
- access=mmap.ACCESS_COPY)) as m:
- print 'Memory Before:'
- print m.readline().rstrip()
- print 'File Before :'
- print f.readline().rstrip()
- print
- m.seek(0) # rewind
- loc = m.find(word)
- m[loc:loc + len(word)] = reversed
- m.seek(0) # rewind
- print 'Memory After :'
- print m.readline().rstrip()
- f.seek(0)
- print 'File After :'
- print f.readline().rstrip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement