Files & data
Read and write files safely; parse JSON, CSV, and common text formats.
Convert Image to ASCII Art in Python
Convert any image to ASCII art by resizing, converting to grayscale, and mapping pixel brightness to characters using Pillow.
from PIL import Image
import sys
ASCII_CHARS = "@%#*+=-:. "
def resize_image(image, new_width=100):
"""Resize image maintaining aspect ratio."""
width, height = image.size
ratio = height / width
new_height = int(new_width * ratio * 0.55) # 0.55 adjusts for font aspect ratio
return image.resize((…
How to Read Binary File Bytes and Inspect the Header in Python
Read the first bytes of a binary file with pathlib and display them as a hex dump plus an ASCII view to inspect file headers.
import pathlib
def inspect_binary_header(filepath: str, num_bytes: int = 16) -> None:
"""Read the first bytes of a binary file and display them as hex and ASCII."""
path = pathlib.Path(filepath)
data = path.read_bytes()[:num_bytes]
hex_str = ' '.join(f"{byte:02x}" for byte in data)
ascii_str …
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.