Reference library

Files & data

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

1 match
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

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.