Advertisement
rfmonk

mmap_write_slice_Errno2IOError.py

Jan 28th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. #1/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7. import mmap
  8. import shutil
  9. import contextlib
  10.  
  11. # Copy the example file
  12. shutil.copyfile('lorem.txt', 'lorem_copy.txt')
  13.  
  14. word = 'consectetuer'
  15. reversed = word[::-1]
  16. print 'Looking for    :', word
  17. print 'Replacing with :', reversed
  18.  
  19. with open('lorem_copy.text', 'r+') as f:
  20.     with contextlib.closing(mmap.mmap(f.fileno(), 0)) as m:
  21.         print 'Before:'
  22.         print m.readline().rstrip()
  23.         m.seek(0)  # rewind
  24.  
  25.         loc = m.find(word)
  26.         m[loc:loc + len(word)] = reversed
  27.         m.flush()
  28.  
  29.         m.seek(0)  # rewind
  30.         print 'After :'
  31.         print m.readline().rstrip()
  32.  
  33.         f.seek(0)  # rewind
  34.         print 'File  :'
  35.         print f.readline().rstrip()
  36.  
  37. """
  38. $ python mmap_write_slice.py
  39. Looking for    : consectetuer
  40. Replacing with : reutetcesnoc
  41. Traceback (most recent call last):
  42. File "mmap_write_slice.py", line 19, in <module>
  43. with open('lorem_copy.text', 'r+') as f:
  44. IOError: [Errno 2] No such file or directory: 'lorem_copy.text'
  45. """
  46. # This is wierd, checked over and over for typos
  47. # I can see that the file exists
  48. # Tried using absolute path names and got the same error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement