Memory-Mapped Files

Carson West

Handling Binary Files

Memory-Mapped Files

Python’s mmap module allows for memory-mapping files. This means treating a file on disk as if it were directly accessible in your program’s memory. This can offer significant performance advantages for large files, especially when dealing with random access.

Key Concepts:

Example:

import mmap
import os

filename = "my_large_file.dat"

# Create a large file for demonstration (optional)
with open(filename, "wb") as f:
    f.write(b"A" * (1024 * 1024 * 10)) # 10MB file

try:
    with open(filename, "r+b") as f:  # Open in read-write binary mode
        mm = mmap.mmap(f.fileno(), 0) # Map the entire file

        # Access and modify the file's contents directly through mm
        print(mm[:10]]) # Read the first 10 bytes

        mm[0:10]] = b"Hello, world!" # Write to the first 10 bytes

        mm.close()  # Close the mapped file
except OSError as e:
    print(f"Error mapping file: {e}")
finally:
    # Ensure the file is closed and resources are released.
    # If the file was newly created, you might need to delete it here
    # if os.path.exists(filename):
    #   os.remove(filename)

Further Exploration:

Important Notes: