Reference library

Python Code Samples

Medium snippets you can copy, study, and run in the browser editor.

636 matches
Files & data medium

Create a ZIP Archive of a Folder in Python

Recursively zip all files in a folder into a single archive using the standard library zipfile and pathlib modules.

zipfile pathlib archives
Python
import zipfile
from pathlib import Path

def zip_folder(source_dir: str, archive_path: str) -> None:
    """Zip all files in source_dir recursively into archive_path."""
    source = Path(source_dir)
    with zipfile.ZipFile(archive_path, "w", zipfile.ZIP_DEFLATED) as archive:
        for file_path in source.rglob("*"…
15 0 Open
Files & data medium

Download Files from Internet with Progress Bar in Python

Download a file from the internet while displaying a text progress bar in the terminal.

urllib download progress bar
Python
import urllib.request
import sys

def download_with_progress(url, filename):
    """Download a file with a simple text progress bar."""
    def report_hook(block_count, block_size, total_size):
        downloaded = block_count * block_size
        if total_size > 0:
            percent = min(100, int(downloaded * 100 …
52 0 Open
Files & data medium

Encrypt and Decrypt Files Using Python

Encrypt and decrypt files using the cryptography library's Fernet symmetric encryption.

encryption decryption fernet
Python
import os
from pathlib import Path
from cryptography.fernet import Fernet

def generate_key(key_file: Path) -> bytes:
    key = Fernet.generate_key()
    key_file.write_bytes(key)
    return key

def load_key(key_file: Path) -> bytes:
    return key_file.read_bytes()

def encrypt_file(input_path: Path, key: bytes, out…
56 0 Open
Files & data medium

Extract Hyperlinks from Word Documents in Python

Parses a .docx file using Python's standard library to extract every hyperlink's display text and target URL.

docx hyperlinks xml
Python
import zipfile
from pathlib import Path
import xml.etree.ElementTree as ET

def extract_hyperlinks_from_docx(filepath: str) -> list[dict]:
    """
    Extract all hyperlinks from a .docx file.
    Returns a list of dicts with 'text' and 'target' keys.
    """
    hyperlinks = []
    with zipfile.ZipFile(Path(filepath)…
88 0 Open
Files & data medium

Find Duplicate Web Pages by Content Similarity in Python

Compute SHA-256 hashes of file contents to detect and report duplicate HTML pages or any files in a directory.

duplicate-detection hashing sha256
Python
import hashlib
import os
from collections import defaultdict

def get_file_hash(filepath):
    """Compute SHA-256 hash of file contents."""
    sha256 = hashlib.sha256()
    with open(filepath, 'rb') as f:
        for chunk in iter(lambda: f.read(4096), b''):
            sha256.update(chunk)
    return sha256.hexdiges…
45 0 Open
Files & data medium

Generate a Beautiful Folder Tree Visualization in Python

A Python utility that creates a visual tree of a directory structure, excluding common files, with configurable depth.

folder tree directory visualization
Python
import os
from pathlib import Path

class FolderTree:
    def __init__(self, root_path=".", ignore_list=None, max_depth=3):
        self.root = Path(root_path)
        self.ignore = set(ignore_list or [".git", "__pycache__", ".DS_Store"])
        self.max_depth = max_depth
        
    def generate(self):
        tree…
67 0 Open
Files & data medium

Generate a Monthly Calendar PDF in Python

Create a Python utility that generates a monthly calendar PDF using ReportLab, with weekday headers and day numbers laid out in a grid.

calendar pdf reportlab
Python
from calendar import TextCalendar
from datetime import datetime
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import os

def generate_monthly_calendar_pdf(year, month, filename="calendar.pdf"):
    cal = TextCalendar()
    days = cal.monthdays2calendar(year, month)
    
    month_name …
1767 0 Open
Files & data medium

How to Atomically Write Files in Python with Temp File and Rename

Write a file atomically using a temporary file and os.replace so readers never see partial writes even if the process crashes mid-write.

atomic-write tempfile fsync
Python
import os
import tempfile
from pathlib import Path

def atomic_write(path: str | Path, content: str) -> None:
    """Write content to path atomically using a temp file and rename."""
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)

    fd, temp_path = tempfile.mkstemp(
        dir=str(path.par…
17 0 Open
Files & data medium

How to Audit Environment Variable Files for Missing Values in Python

A Python tool that reads an environment variable file and reports any variables with empty or missing values.

environment-variables file-audit configuration
Python
import os
import re
from pathlib import Path

def audit_env_file(filepath: str) -> None:
    """
    Audit an environment variable file for missing values.
    Prints file status and lists variables that have empty values.
    """
    path = Path(filepath)
    if not path.exists():
        print(f"Error: File '{filepa…
39 0 Open
Files & data medium

How to Automatically Extract Every Archive in a Folder with Python

Walk through a folder and extract all ZIP, RAR, and 7Z archives into separate subdirectories using Python.

zipfile rarfile py7zr
Python
import zipfile
import rarfile
import py7zr
import pathlib

def extract_archives(folder: str):
    """Extract every ZIP, RAR, and 7Z archive in the given folder."""
    folder_path = pathlib.Path(folder)
    for archive_file in folder_path.iterdir():
        suffix = archive_file.suffix.lower()
        try:
           …
36 0 Open
Files & data medium

How to Automatically Merge Hundreds of Excel Files Without Losing Formatting in Python

Merge all .xlsx files in a folder into a single Excel workbook, preserving individual sheet structures with sheet name prefixes.

excel pandas merge
Python
import pandas as pd
from pathlib import Path

def merge_excel_files(folder_path: str, output_path: str) -> None:
    """
    Merge all .xlsx files in a folder into a single Excel file,
    preserving individual sheet structures.
    """
    folder = Path(folder_path)
    excel_files = list(folder.glob("*.xlsx"))
    
…
42 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 medium

How to Compare Two Files by Content Hash Equality in Python

Compares two files by hashing their contents with SHA-256, skipping the hash if file sizes differ, and returns whether they are identical.

hashlib sha256 file-hashing
Python
import hashlib
from pathlib import Path

def file_hash(path: Path, chunk_size: int = 8192) -> str:
    sha256 = hashlib.sha256()
    with path.open("rb") as f:
        for chunk in iter(lambda: f.read(chunk_size), b""):
            sha256.update(chunk)
    return sha256.hexdigest()

def files_are_identical(file_a: Pat…
13 0 Open
Files & data medium

How to Find Duplicate Files by Size and Hash in Python

Recursively scan a directory, group files by size, then hash candidates to identify exact duplicate files.

deduplication filesystem hashlib
Python
import hashlib
from pathlib import Path

def hash_file(path, chunk_size=8192):
    hasher = hashlib.md5()
    with open(path, 'rb') as f:
        while chunk := f.read(chunk_size):
            hasher.update(chunk)
    return hasher.hexdigest()

def find_duplicates(directory):
    size_map = {}
    for path in Path(dir…
16 0 Open
Files & data medium

How to Generate Beautiful QR Codes with Embedded Logos in Python

Generate a high-error-correction QR code and paste a logo image in the center to create a branded, scannable QR code.

qrcode qrcode-generation pillow
Python
import qrcode
from PIL import Image

def generate_qr_with_logo(data, logo_path, output_path):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)

    qr_img = qr.make_image(fill_c…
50 0 Open
Files & data medium

How to Generate an Inventory Report of All Files in Python

Walk a directory tree, collect metadata for every file, and write a CSV inventory report using Python's os, pathlib, and csv modules.

os.walk pathlib csv
Python
import os
import csv
from pathlib import Path
from datetime import datetime

def generate_inventory_report(root_dir: str = "/", output_file: str = "inventory_report.csv"):
    headers = ["File Path", "Size (bytes)", "Last Modified", "File Type"]
    rows = []
    start_time = datetime.now()
    
    for dirpath, dirna…
47 0 Open
Files & data medium

How to Load Pickle Files Safely in Python

This code demonstrates how to load pickle files safely in Python by using a restricted unpickler that only allows specific, trusted classes, preventing arbitrary code execution from untrusted pickles.

pickle security serialization
Python
import pickle

# Default pickle.load is unsafe: it executes arbitrary code when unpickling.
class Unsafe:
    def __reduce__(self):
        return (eval, ("open('/tmp/pickle_demo.txt', 'w').write('pwned')",))

# Create a malicious payload (simulating untrusted source)
malicious_data = pickle.dumps(Unsafe())

# Safe ap…
13 0 Open
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 Merge Sorted Chunk Files in Python

Merge multiple sorted text files into one sorted output file using a heap for efficient k-way merging.

heapq merge-sort external-sort
Python
import heapq


def merge_sorted_chunks(chunks, output_path):
    """Merge multiple sorted iterables into single sorted output file."""
    with open(output_path, "w") as out_f:
        # Open all chunk files
        handles = [open(chunk, "r") for chunk in chunks]
        try:
            # Heap of (value, index) tupl…
13 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 …
14 0 Open
Files & data medium

How to Scrape Headlines from a News Website Using Beautiful Soup in Python

Scrape headline text from a news website using requests and Beautiful Soup with a CSS selector.

web scraping beautifulsoup requests
Python
import requests
from bs4 import BeautifulSoup

def scrape_headlines(url: str, selector: str) -> list:
    """
    Scrape headlines from a news website using Beautiful Soup.
    
    Args:
        url: The URL of the news website.
        selector: CSS selector for headline elements.
    
    Returns:
        List of h…
55 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
Files & data medium

How to Sync Two Folders in Python (Lightweight Backup)

A Python script that synchronizes a source folder to a destination folder, copying new or updated files and removing files that no longer exist in the source.

sync backup filesystem
Python
import os
import shutil
import sys
from pathlib import Path

def sync_folders(src: Path, dst: Path):
    """Sync src folder to dst folder, copying missing/updated files."""
    dst.mkdir(parents=True, exist_ok=True)

    for src_path in src.rglob("*"):
        relative = src_path.relative_to(src)
        dst_path = ds…
37 0 Open
Files & data medium

How to Use fcntl for Exclusive File Locking in Python

This code demonstrates how to acquire an exclusive advisory lock on a file using fcntl.flock with a non-blocking flag, simulate work, then release the lock.

fcntl file-locking flock
Python
import fcntl
import os
import tempfile
import time

def acquire_exclusive_lock(filepath):
    fd = os.open(filepath, os.O_RDWR | os.O_CREAT)
    try:
        fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
        print(f"Exclusive lock acquired on {filepath}")
        time.sleep(1)  # Simulate work while holding the l…
11 0 Open

Browse by section

Each section groups closely related Python snippets.

Guide: free Python code samples library

Copy-ready Python snippets for learners and developers

PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.

How to use this library

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. Run it in the IDE, tweak values, then take a related quiz or tutorial lesson

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.