[code.view]

[top] / python / PyMOTW / mmap / mmap_write_slice.py

     #!/usr/bin/env python
     #
     # Copyright 2007 Doug Hellmann.
     #
     """Writing to a memory mapped file using a slice assignment.
     
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     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.txt', 'r+') as f:
         with contextlib.closing(mmap.mmap(f.fileno(), 0)) as m:
             print 'Before:', 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 :', m.readline().rstrip()
     

[top] / python / PyMOTW / mmap / mmap_write_slice.py

contact | logmethods.com