peek() lets you look ahead in a binary file: it returns bytes from the read buffer but leaves the position where it was, so the next read() still returns those same bytes.
The amount it returns is not fixed: it is whatever the buffer holds, filled with one raw read if it was empty. Slice the result, as in f.peek(4)[:4], when you need an exact number.
peek() is a method of binary buffered readers: io.BufferedReader (from open(path, "rb")) and io.BufferedRandom ("r+b"). Text files, BufferedWriter, raw FileIO, and io.BytesIO do not have it. No import is needed.
Syntax
Call it on a file opened with "rb" or "r+b", then slice to the length you need.
python
ahead = f.peek()
first_four = f.peek(4)[:4]
Call
What it does
f.peek(4)[:4]
The next four bytes (fewer near the end), position unchanged.
f.peek()
Everything currently buffered from the position on; may be many bytes.
f.peek(1)
At the end of the file: returns b''.
The size argument is only a hint: CPython may return more or fewer bytes, but never moves the position.
Method signatures
python
BufferedReader.peek(size=0, /) # -> bytes, position unchanged
BufferedRandom.peek(size=0, /) # -> bytes, position unchanged
# documented as peek([size]); the result length may differ from size
Parameters
Parameter
Description
size
Optional hint for how many bytes you want. The result may be longer or shorter, so slice it yourself.
Return Value
Return Value
Description
bytes
Bytes starting at the current position, copied from the buffer. At most one raw read happens, and only if the buffer was empty.
b''
At the end of the file.
Python Examples
Each example works inside a temporary folder that is deleted at the end, so nothing is left on your disk.
Example 1 — Check a PNG Signature Before Reading
Peek at the first eight bytes, compare them with the PNG signature, then read the file from the start.
python
import os
import tempfile
PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "icon.png")
with open(path, "wb") as f:
f.write(PNG_SIGNATURE + b"IHDR")
with open(path, "rb") as f:
head = f.peek(8)[:8]
print(head == PNG_SIGNATURE, f.tell())
data = f.read()
print(len(data), data[:4])
Output
True 0
12 b'\x89PNG'
How It Works
Magic number. Real PNG files start with these eight bytes, so a fake 12-byte file with the signature plus IHDR is enough to test.
Exact slice.f.peek(8) may return more than eight bytes; [:8] keeps exactly the signature-sized prefix.
Position untouched.tell() is still 0 after the peek, so nothing was consumed.
Full read afterwards.read() returns all 12 bytes, starting with the very bytes that were peeked.
Example 2 — Detect JSON or CSV from the First Byte
Two price files: decide the format from one peeked byte, then start reading normally.
python
import tempfile
from pathlib import Path
samples = {"prices.json": b'{"tea": 3}', "prices.csv": b"item,price\ntea,3\n"}
with tempfile.TemporaryDirectory() as folder:
for name, content in samples.items():
path = Path(folder) / name
path.write_bytes(content)
with open(path, "rb") as f:
first = f.peek(1)[:1]
kind = "json" if first == b"{" else "csv"
print(name, "->", kind, f.read(4))