Reference library

Files & data

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

25 matches
Files & data easy

Build a File Index by Relative Path Hash Map in Python

Recursively walk a directory and map normalized relative paths to absolute file paths using a defaultdict hash map.

os.walk file-index defaultdict
Python
import os
from collections import defaultdict


def build_file_index(root_dir):
    index = defaultdict(list)

    for dirpath, dirnames, filenames in os.walk(root_dir):
        for filename in filenames:
            full_path = os.path.join(dirpath, filename)
            relative_path = os.path.relpath(full_path, roo…
18 0 Open
Files & data easy

Convert File Data to a Dictionary in Python

This function scans a directory and converts each file's metadata (name, size, extension) into a structured dictionary for easy access.

file-metadata pathlib directory
Python
from pathlib import Path

def convert_files_data(directory: str) -> dict:
    data = {}
    base = Path(directory)
    if not base.exists():
        return data
    for file in base.iterdir():
        if file.is_file():
            data[file.name] = {
                "size": file.stat().st_size,
                "exten…
15 0 Open
Files & data easy

Export List of Dicts to CSV in Python

Write a list of dictionaries (dataframe-like) to a CSV file with headers using the standard library csv module and verify by reading it back.

csv export dictwriter
Python
import csv

def export_to_csv(data, filename):
    """Export a list of dicts to a CSV file."""
    if not data:
        print("No data to export")
        return
    
    # Get column names from the keys of the first dict
    fieldnames = list(data[0].keys())
    
    with open(filename, 'w', newline='', encoding='utf…
14 0 Open
Files & data easy

Generate Timesheet Reports from Daily Logs in Python

Aggregate daily log entries by project and produce a formatted timesheet report using Python's standard library.

timesheet reporting aggregation
Python
import json
from pathlib import Path
from collections import defaultdict

def generate_timesheet_report(daily_logs: list[dict]) -> str:
    """
    Generate a timesheet report from daily log entries.
    
    Args:
        daily_logs: List of dicts with 'date', 'project', 'hours', 'task' keys
    
    Returns:
       …
45 0 Open
Files & data medium

How to Build a CSV Comparison Tool That Highlights Every Changed Cell in Python

Read two CSV files with DictReader, compare cell by cell, and return a list of dictionaries describing each changed cell using only the standard library.

csv comparison diff
Python
import csv
from pathlib import Path

def csv_cell_diff(file_a: str, file_b: str) -> list[dict]:
    rows_a = list(csv.DictReader(Path(file_a).open('r', newline='')))
    rows_b = list(csv.DictReader(Path(file_b).open('r', newline='')))
    if not rows_a or not rows_b:
        return []
    columns = list(rows_a[0].key…
40 0 Open
Files & data easy

How to Group Files by Extension in Python

Group file names by their file extension using a dictionary and pathlib, producing a simple clear mapping for beginners.

pathlib grouping filesystem
Python
from pathlib import Path


def group_data_by_extension(files: list[Path]) -> dict[str, list[str]]:
    """Group file names by their extension."""
    grouped: dict[str, list[str]] = {}
    for file in files:
        ext = file.suffix.lower()
        grouped.setdefault(ext, []).append(file.name)
    return grouped


if…
14 0 Open
Files & data easy

How to List File Information in a Directory with Python

A helper that walks a directory and returns each file's name, size, and extension as a list of dictionaries.

pathlib filesystem file-metadata
Python
from pathlib import Path


def get_files_data(directory: str) -> list[dict]:
    """Return basic info about all files in a directory."""
    files = []
    for path in Path(directory).iterdir():
        if path.is_file():
            files.append({
                "name": path.name,
                "size": path.stat()…
12 0 Open
Files & data easy

How to List File Metadata in Python

This code walks a directory and returns a list of JSON-ready dicts with each file's name, size, and modification time.

pathlib file-metadata filesystem
Python
from pathlib import Path
import json

def format_files_data(directory_path):
    """Return a list of JSON-serializable dicts with file metadata."""
    base = Path(directory_path)
    if not base.is_dir():
        raise ValueError(f"Not a directory: {directory_path}")

    files_data = []
    for file_path in base.ite…
12 0 Open
Files & data easy

How to Merge Dicts from Two JSON Files Like a Pro

This helper reads two JSON files that contain dicts, merges them with the second file overriding duplicate keys, and saves the result to a new file.

json dict merge
Python
import json
from pathlib import Path


def merge_json_files(file1: str, file2: str, output: str = "merged.json") -> dict:
    """Merge two JSON files containing dicts, with file2 overriding file1."""
    data1 = json.loads(Path(file1).read_text())
    data2 = json.loads(Path(file2).read_text())

    merged = {**data1,…
13 0 Open
Files & data easy

How to Merge Environment-Specific Config JSON in Python

Loads a base JSON config and overlays environment-specific overrides, merging the two dictionaries into one final config.

json config pathlib
Python
import json
import pathlib


def load_config(base_path: pathlib.Path, env: str) -> dict:
    base_config = json.loads(base_path.read_text())
    env_path = base_path.with_name(f"config.{env}.json")
    if env_path.exists():
        env_config = json.loads(env_path.read_text())
        return {**base_config, **env_conf…
14 0 Open
Files & data medium

How to Parse Apache Log Files in Python

Parse Apache common log format lines into structured dictionaries using Python's standard library.

apache regex log-parsing
Python
import re
from pathlib import Path

def parse_apache_line(line):
    pattern = r'^(\S+) (\S+) (\S+) \[([^\]]+)\] "(\S+) (\S+) (\S+)" (\d{3}) (\S+)'
    match = re.match(pattern, line)
    if not match:
        return None
    ip, ident, user, timestamp, method, path, protocol, status, size = match.groups()
    return …
15 0 Open
Files & data easy

How to Parse XML Attributes into a Flat Dictionary in Python

Parses XML elements and attributes using ElementTree, building a flat dictionary keyed by element attributes.

xml elementtree parsing
Python
import xml.etree.ElementTree as ET

xml_data = """<root>
    <book id="1" category="fiction" price="9.99">
        <title>The Catcher</title>
    </book>
    <book id="2" category="nonfiction" price="12.50">
        <title>Deep Learning</title>
    </book>
</root>"""

def parse_xml_attributes(xml_string):
    root = E…
14 0 Open
Files & data easy

How to Read a JSON File into a Dictionary in Python

Load a JSON file into a Python dictionary using the json.load() function with proper file handling and UTF-8 encoding.

json file-io dictionary
Python
import json
from pathlib import Path

def read_json_file(filepath: str) -> dict:
    """Read a JSON file and return its contents as a dictionary."""
    path = Path(filepath)
    with path.open("r", encoding="utf-8") as f:
        data = json.load(f)
    return data

if __name__ == "__main__":
    # Create a sample JS…
13 0 Open
Files & data easy

How to Read a TSV File in Python with csv.DictReader

Read a tab-separated (TSV) file into dictionaries using the csv module's DictReader with a tab delimiter.

csv tsv file-io
Python
import csv
from pathlib import Path

data_file = Path("data.tsv")

# Sample TSV content (tab-separated)
sample = """name\tage\tcity
Alice\t30\tNew York
Bob\t25\tLos Angeles
Carol\t35\tChicago
"""
data_file.write_text(sample)

with data_file.open("r", newline="", encoding="utf-8") as f:
    reader = csv.DictReader(f, d…
14 0 Open
Files & data easy

How to Sort Files by Name and Size in Python

Sort a list of file dictionaries by name then size using Python's sorted() with a lambda key.

sorting files lambda
Python
from pathlib import Path

def sort_files_data(files):
    """Sort a list of file dictionaries by name, then by size."""
    return sorted(files, key=lambda f: (f["name"], f["size"]))

if __name__ == "__main__":
    files_data = [
        {"name": "report.pdf", "size": 2048},
        {"name": "data.csv", "size": 1024},…
13 0 Open
Files & data easy

How to Split Files by Extension in Python

Group files in a folder by their file extension into a dictionary using pathlib.

pathlib filesystem grouping
Python
from pathlib import Path

def split_files_by_extension(folder_path):
    folder = Path(folder_path)
    files_by_ext = {}

    for file_path in folder.iterdir():
        if file_path.is_file():
            ext = file_path.suffix.lower() or "no_extension"
            files_by_ext.setdefault(ext, []).append(file_path.na…
12 0 Open
Files & data easy

How to Sum a CSV Column by Group in Python

This code reads a CSV string and sums a specified column for each unique value of a group key using the csv module and defaultdict.

csv aggregation data-summary
Python
import csv
from collections import defaultdict
from io import StringIO

def aggregate_csv(csv_data, group_key, sum_column):
    totals = defaultdict(float)
    reader = csv.DictReader(StringIO(csv_data))
    for row in reader:
        key = row[group_key]
        totals[key] += float(row[sum_column])
    return dict(t…
12 0 Open
Files & data easy

How to Validate a JSON File in Python

A beginner-friendly Python helper that reads a JSON file, catches common errors, and returns a status dictionary.

json validation file-handling
Python
import json
from pathlib import Path

def get_valid_json_data(file_path: str) -> dict:
    file = Path(file_path)
    if not file.exists():
        return {"status": "error", "message": f"File not found: {file_path}"}
    
    try:
        data = json.loads(file.read_text())
    except json.JSONDecodeError as e:
     …
12 0 Open
Files & data easy

How to Write a Dict to a Pretty JSON File with Indent in Python

Serializes a Python dictionary to a readable JSON file using json.dump with indentation and sorted keys, then prints the file contents to stdout.

json files serialization
Python
import json
from pathlib import Path

data = {
    "name": "Python",
    "version": 3.12,
    "features": ["simple", "readable", "powerful"],
    "nested": {"creator": "Guido van Rossum", "year": 1991}
}

output_path = Path("output.json")

with output_path.open("w", encoding="utf-8") as f:
    json.dump(data, f, inden…
14 0 Open
Files & data medium

Join two CSV files on shared key column in Python

Merge rows from two CSV files by a common key column, outputting combined records to a new file.

csv join dictreader
Python
import csv

def join_csv(file1, file2, key, output="joined.csv"):
    # Read first CSV into dict keyed by the join column
    with open(file1, newline="") as f1:
        reader1 = csv.DictReader(f1)
        data1 = {row[key]: row for row in reader1}

    # Read second CSV and merge matching rows
    with open(file2, n…
15 0 Open
Files & data easy

Parse Fixed Width Data File by Column Slices in Python

Extract fields from fixed-width text by slicing each line at defined column offsets, with a dictionary describing the boundaries.

fixed-width string-slicing parsing
Python
from pathlib import Path


def parse_fixed_width(data: str, slices: dict[str, tuple[int, int]]) -> list[dict[str, str]]:
    lines = data.strip().splitlines()
    records = []
    for line in lines:
        record = {}
        for name, (start, end) in slices.items():
            record[name] = line[start:end].strip()…
13 0 Open
Files & data medium

Read Parquet-Like Columnar CSV Chunks in Python

A Python generator that reads a CSV file column-by-column, yielding dictionary chunks where each key points to a list of values—mirroring how Parquet stores data columnar.

csv columnar generator
Python
```python
import csv
from pathlib import Path
from typing import Iterator, List

def read_parquet_like_columnar(csv_path: str, column_names: List[str], chunk_size: int = 2) -> Iterator[dict]:
    """Read CSV data in columnar chunks, similar to how parquet stores columns."""
    csv_file = Path(csv_path)
    with csv_f…
13 0 Open
Files & data easy

Read SQLite database with sqlite3 module in Python

Connect to a SQLite database and query rows with the standard library sqlite3 module, returning results as dictionaries.

sqlite database stdlib
Python
import sqlite3
from pathlib import Path

# Create an in-memory database and a sample table
connection = sqlite3.connect(":memory:")
cursor = connection.cursor()

cursor.execute("""
CREATE TABLE employees (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    department TEXT NOT NULL,
    salary REAL
)
""")

# Inser…
17 0 Open
Files & data easy

Read a CSV File with csv.DictReader in Python

Read a CSV file as a list of dictionaries, using csv.DictReader to map each row to column names.

csv csv-dictreader file-reading
Python
import csv
from pathlib import Path

def read_csv_with_dictreader(file_path):
    data = []
    with open(file_path, mode='r', newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            data.append(row)
    return data

if __name__ == "__main__":
    # Cre…
10 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.