Files & data
Read and write files safely; parse JSON, CSV, and common text formats.
Compress and Extract ZIP Files Programmatically in Python
Create a ZIP archive with in-memory files and extract its contents to a directory using Python's stdlib zipfile and pathlib modules.
import zipfile
from pathlib import Path
import tempfile
import os
def create_sample_zip(zip_path: str, files: dict) -> None:
"""Create a ZIP file containing the given files (name -> content mapping)."""
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
for filename, content in files.ite…
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.
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("*"…
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.
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)…
Extract a Single Member from a ZIP Archive in Python
Extract one specific file from a ZIP archive to an output directory using the standard zipfile and pathlib modules.
import zipfile
from pathlib import Path
def extract_single_member(zip_path: str, member_name: str, output_dir: str = ".") -> Path:
"""Extract a single member from a zip archive to the output directory."""
with zipfile.ZipFile(zip_path, "r") as archive:
archive.extract(member_name, output_dir)
retu…
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.
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:
…
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.