Reference library

Python Code Samples

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

5 matches
Files & data medium

Create a Local File Versioning System Using Pure Python

Track file changes locally by copying versions with SHA-256 hashes and JSON metadata using only the Python standard library.

file-versioning files backup
Python
import os
import shutil
import hashlib
import json
import time
from pathlib import Path

class LocalFileVersioning:
    def __init__(self, target_dir="versioned_files", versions_dir="versions"):
        self.target_dir = Path(target_dir)
        self.versions_dir = Path(versions_dir)
        self.metadata_file = self.…
53 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…
38 0 Open
Automation & scripting medium

How to Sync Two Directories in Python (rsync-like)

Mirror a source directory into a destination by copying new or changed files and deleting extras, similar to rsync.

sync directory rsync
Python
import os
import shutil
import sys
from pathlib import Path

def sync_dirs(src: Path, dst: Path):
    """Mirror src into dst: copy new files, overwrite changed, delete extras."""
    dst.mkdir(parents=True, exist_ok=True)
    for dst_entry in dst.rglob('*'):
        rel = dst_entry.relative_to(dst)
        src_entry =…
14 0 Open
Concurrency & performance medium

How to Use threading.local for Per-Thread Data in Python

Use threading.local to keep thread-specific data — each thread gets its own copy of the attribute, so values don't leak between threads.

threading thread-local concurrency
Python
import threading
import time

local_storage = threading.local()

def worker(name):
    local_storage.name = name
    time.sleep(0.1)
    print(f"Thread {threading.current_thread().name}: {local_storage.name}")

if __name__ == "__main__":
    threads = []
    for i in range(3):
        t = threading.Thread(target=worke…
14 0 Open
Production deployment patterns medium

How to Expand a Contract and Migrate Data in Python

Expand an old data contract by renaming fields and adding defaults, then migrate to a final version with deepcopy isolation.

contract migration deepcopy
Python
import json
from copy import deepcopy

# Mock data representing a user record (old contract)
old_contract = {
    "id": 1,
    "name": "Alice",
    "email": "alice@example.com",
    "age": 30,
    "status": "active"
}

# Expanded contract: adds fields with defaults and renames some fields
expand_rules = {
    "id": "u…
14 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.