O'Reilly logo

Python in a Nutshell, 2nd Edition by Alex Martelli

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The mmap Module

The mmap module supplies memory-mapped file objects. An mmap object behaves similarly to a plain (not Unicode) string, so you can often pass an mmap object where a plain string is expected. However, there are differences:

  • An mmap object does not supply the methods of a string object.

  • An mmap object is mutable, while string objects are immutable.

  • An mmap object also corresponds to an open file and behaves polymorphically to a Python file object (as covered in File-Like Objects and Polymorphism).

An mmap object m can be indexed or sliced, yielding plain strings. Since m is mutable, you can also assign to an indexing or slicing of m. However, when you assign to a slice of m, the righthand side of the assignment statement must be a string of exactly the same length as the slice you’re assigning to. Therefore, many of the useful tricks available with list slice assignment (covered in Modifying a list) do not apply to mmap slice assignment.

Module mmap supplies a factory function that is slightly different on Unix-like systems and Windows.

mmap

mmap(filedesc,length,tagname='') # Windows mmap(filedesc,length,flags=MAP_SHARED, prot=PROT_READ|PROT_WRITE) # Unix

Creates and returns an mmap object m that maps into memory the first length bytes of the file indicated by file descriptor filedesc. filedesc must normally be a file descriptor opened for both reading and writing (except, on Unix-like platforms, when argument prot requests only reading or only writing). (File descriptors ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required