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]
- print 'Looking for :', word
- print 'Replacing with :', reversed
- with open('lorem_copy.text', 'r+') as f:
- with contextlib.closing(mmap.mmap(f.fileno(), 0)) as m:
- print 'Before:'
- print m.readline().rstrip()
- m.seek(0) # rewind
- loc = m.find(word)
- m[loc:loc + len(word)] = reversed
- m.flush()
- m.seek(0) # rewind
- print 'After :'
- print m.readline().rstrip()
- f.seek(0) # rewind
- print 'File :'
- print f.readline().rstrip()
- """
- $ python mmap_write_slice.py
- Looking for : consectetuer
- Replacing with : reutetcesnoc
- Traceback (most recent call last):
- File "mmap_write_slice.py", line 19, in <module>
- with open('lorem_copy.text', 'r+') as f:
- IOError: [Errno 2] No such file or directory: 'lorem_copy.text'
- """
- # This is wierd, checked over and over for typos
- # I can see that the file exists
- # Tried using absolute path names and got the same error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement