Python Mmap Module
The mmap module provides memory-mapped file objects. A memory-mapped file behaves like both a bytearray and a file object. You can use mmap objects in most places where bytearray is expected; for example, you can use the re module to search through a memory-mapped file.
Importing the Module
import mmap
Creating a Memory-Mapped File
To map a file into memory, you first need to open the file with a mode that allows updates (usually r+b or w+b).
import mmap
# Create a sample file
with open("example.txt", "wb") as f:
f.write(b"Hello Python!")
with open("example.txt", "r+b") as f:
# memory-map the file, size 0 means whole file
# access=mmap.ACCESS_WRITE specifies write-through
with mmap.mmap(f.fileno(), 0) as mm:
# Read content via standard file methods
print(mm.read(5)) # Output: b'Hello'
# Read content via slice notation
print(mm[:5]) # Output: b'Hello'
Modifying Data
You can modify the file content using slice notation or the write() method.
with open("example.txt", "r+b") as f:
with mmap.mmap(f.fileno(), 0) as mm:
# Update content using slice notation
# Note: new content must have the same size as the slice
mm[6:12] = b"World!"
mm.seek(0)
print(mm.readline()) # Output: b'Hello World!!'
Access Modes
ACCESS_READ: Opens the mmap for reading only.ACCESS_WRITE: Opens the mmap for writing (changes affect the underlying file).ACCESS_COPY: Opens the mmap for copy-on-write (changes do not affect the underlying file).
with open("example.txt", "r") as f:
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
print(mm.readline())
Searching
Since mmap objects behave like bytearrays, you can search them directly.
with open("example.txt", "r+b") as f:
with mmap.mmap(f.fileno(), 0) as mm:
if mm.find(b"Python") != -1:
print("Found 'Python'")