Files & data
Read and write files safely; parse JSON, CSV, and common text formats.
Build a Simple ETL Pipeline in Python
A simple ETL pipeline that reads JSON Lines, transforms records with filtering and normalization, and writes the result to JSON.
import json
from pathlib import Path
def read_input(file_path: Path) -> list[dict]:
"""Read JSON lines file into list of dicts."""
with file_path.open("r", encoding="utf-8") as f:
return [json.loads(line) for line in f if line.strip()]
def transform(records: list[dict]) -> list[dict]:
"""Transf…
How to Filter Files by Extension and Size in Python
Use pathlib to list files in a directory, filter by extension or minimum size, and return matching names or (name, size) pairs.
from pathlib import Path
def filter_files_by_extension(directory: str, extension: str) -> list:
"""Return a list of file names in directory with the given extension."""
path = Path(directory)
return [f.name for f in path.iterdir() if f.is_file() and f.suffix == extension]
def filter_files_by_size(directo…
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.