Reference library

Files & data

Read and write files safely; parse JSON, CSV, and common text formats.

2 matches
Files & data medium

How to Memory Map Large Files Read-Only in Python

This code demonstrates reading only the tail of a large file using a read-only memory map (mmap) to avoid loading the entire file into memory.

mmap file-io memory-efficient
Python
import mmap
import os

def read_tail_with_mmap(filepath, bytes_from_end=64):
    """Read the last bytes of a large file using a read-only mmap."""
    file_size = os.path.getsize(filepath)
    start = max(0, file_size - bytes_from_end)

    with open(filepath, "rb") as f:
        with mmap.mmap(f.fileno(), length=0, a…
12 0 Open
Files & data medium

How to Stream Large CSV Files in Python

Process a large CSV file in memory-efficient chunks using Python's csv module, yielding batches of rows instead of loading everything at once.

csv streaming memory-efficient
Python
import csv
from pathlib import Path

def process_csv_in_chunks(file_path, chunk_size=1000):
    """Yield rows from a large CSV file in chunks without loading all into memory."""
    with open(file_path, 'r', newline='') as f:
        reader = csv.DictReader(f)
        chunk = []
        for row in reader:
            …
12 0 Open

Browse by section

Each section groups closely related Python snippets.

Files & data — Python code examples

What you will find here

This page collects files & data snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.