Skip to main content

How to Treat Bytes as a File in Python with io.BytesIO

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Some Python code only reads from files, but sometimes all you have is bytes in memory, such as a download you haven't saved. io.BytesIO wraps bytes in an object that behaves like an open file, so that code can read them without anything touching the disk. This guide reads from one on Python 3.15.2.

Syntax​

Syntax
import io

<file> = io.BytesIO(<bytes>)
PartWhat it means
<bytes>The data, such as b"..." or the bytes a download returned
<file>An in-memory file: read, readline, seek and the rest work on it

The io reference calls it "A binary stream using an in-memory bytes buffer."

The Example​

Two lines of text, as bytes, go into a BytesIO. The script reads the first line, then the rest, then goes back to the start and reads everything again.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

bytes_file.py
import io

file = io.BytesIO(b"first line\nsecond line\n")

print(file.readline())
print(file.read())
file.seek(0)
print(file.read())

The bytes_file.py script in a macOS-style code window: file = io.BytesIO(b"first line\nsecond line\n"), then readline, read, seek(0) and read again.

Step 2: Run It​

Terminal
uv run python bytes_file.py
Output
b'first line\n'
b'second line\n'
b'first line\nsecond line\n'

The terminal after uv run python bytes_file.py: b'first line\n', then b'second line\n', then both lines together.

readline returned the first line and moved the position past it, so read returned only what was left. seek(0) moved back to the start, and read returned everything. Like a real file, it hands back bytes. The command works unchanged in PowerShell.

Where It Helps​

zipfile.ZipFile expects a file. With BytesIO, it reads a zip that exists only in memory. This script builds a zip in memory, takes its bytes, and opens them again:

zip_in_memory.py
import io
import zipfile

buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as archive:
archive.writestr("hello.txt", "Hello from memory")

data = buffer.getvalue()
with zipfile.ZipFile(io.BytesIO(data)) as archive:
print(archive.read("hello.txt"))
Output
b'Hello from memory'

The same zipfile.ZipFile(io.BytesIO(data)) reads a zip you've downloaded, without saving it first.

Common Mistakes​

MistakeWhat happensDo this instead
Giving it text: io.BytesIO("first line")TypeError: a bytes-like object is required, not 'str'Pass bytes, b"first line", or use io.StringIO for text
Reading again without seek(0)Two read() calls in a row returned everything, then b'', because the position was at the endCall seek(0) first

Troubleshooting​

SymptomLikely causeFix
a bytes-like object is required, not 'str'Text was passed instead of bytesEncode it, "text".encode(), or use io.StringIO

FAQ​

What's the text version?​

io.StringIO. io.StringIO("first line\nsecond line\n").readline() returned first line as text.

How do I get the bytes back out?​

With getvalue(), which returns everything in the buffer, as the zip example does.

Conclusion​

io.BytesIO(b"...") behaved like an open file: readline, read and seek worked on it, and zipfile.ZipFile read a zip straight from memory through it. Pass bytes, not text.

Additional Resources​

  • io.BytesIO — every method of the in-memory stream.
  • zipfile — reading zips from files and streams.