You need to seek back to the beginning of the file after writing the initial in memory file...

myio.seek(0)
Answer from mgilson on Stack Overflow
🌐
Python
docs.python.org › 3 › library › io.html
io — Core tools for working with streams
Its subclasses, BufferedWriter, ... interface to seekable streams. Another BufferedIOBase subclass, BytesIO, is a stream of in-memory bytes....
🌐
GeeksforGeeks
geeksforgeeks.org › python › stringio-and-bytesio-for-managing-data-as-file-object
Stringio And Bytesio For Managing Data As File Object - GeeksforGeeks
July 24, 2025 - BytesIO is like a virtual file that exists in the computer's memory, just like `StringIO`. However, it's tailored to handle binary data (bytes) instead of text. It lets you perform operations on these bytes, such as reading and writing, as if ...
Discussions

python - Writing then reading in-memory bytes (BytesIO) gives a blank result - Stack Overflow
I wanted to try out the python BytesIO class. As an experiment I tried writing to a zip file in memory, and then reading the bytes back out of that zip file. So instead of passing in a file-object to More on stackoverflow.com
🌐 stackoverflow.com
Document BytesIO buffer size
https://stackoverflow.com/questions/26827055/python-how-to-get-bytesio-allocated-memory-length The length of the data stored in a BytesIO object can be accessed in several ways. The best way seems ... More on github.com
🌐 github.com
2
May 6, 2025
<class 'bytes'> vs <class '_io.BytesIO'>
These are completely different things. BytesIO is, as the name implies, an input/output object. It doesn't represent the bytes themselves, it represents a stream such as you might read from disk or receive over a network. bytes is the bytes themselves, ie the data that is received via a BytesIO. More on reddit.com
🌐 r/learnpython
4
1
December 1, 2022
How to get a path pointing to io.BytesIO?
I am finding lot of librarys that use the filename for opening the file. However, in my Flask app I would like to get a filename that points to IO.BytesIO without the need o using temporal files More on github.com
🌐 github.com
32
June 3, 2020
🌐
Reddit
reddit.com › r/learnpython › does bytesio need to be closed.
r/learnpython on Reddit: Does BytesIO need to be closed.
January 17, 2023 -

I’m using BytesIO on some of my python scripts. Writing a file into memory to upload it to SharePoint. I think I don’t understand how that works quite enough or what a buffer is.

My concern (possibly unwarranted) is that I need to close the function/buffer once the files been successfully uploaded. Or is that unnecessary and the buffer gets flushed automatically

🌐
Medium
medium.com › @sarthakshah1920 › harnessing-the-power-of-in-memory-buffers-with-bytesio-0ac6d5493178
Harnessing the Power of In-Memory Buffers with BytesIO | by Sarthak Shah | Medium
December 24, 2023 - We write the file content to the in-memory buffer using the write method. In this case, we encode the text content to bytes using the encode() method before writing it to the buffer. This step is essential since BytesIO deals with binary data.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-io-bytesio-stringio
Python io.BytesIO and io.StringIO: Memory File Guide | DigitalOcean
August 3, 2022 - import io stream_str = io.BytesIO(b"JournalDev Python: \x00\x01") print(stream_str.getvalue())
Find elsewhere
🌐
Python⇒Speed
pythonspeed.com › articles › bytesio-reduce-memory-usage
The surprising way to save memory with BytesIO
February 27, 2025 - If you need a file-like object that stores bytes in memory in Python, chances are you you’re using Pytho’s built-in io.BytesIO(). And since you’re already using an in-memory object, if your data is big enough you probably should try to save memory when reading that data back out.
🌐
Medium
medium.com › @abhishekshaw020 › understanding-bytesio-handling-in-memory-files-like-a-pro-e1b767339468
Understanding BytesIO: Handling In-Memory Files Like a Pro | by Abhishek Shaw | Medium
March 31, 2025 - Think of BytesIO as a virtual file that lives in your computer’s memory (RAM) instead of your hard drive. It lets you read and write data just like a normal file, but everything stays in memory—fast, efficient, and no cleanup required!
🌐
GitHub
github.com › python › cpython › issues › 133521
Document BytesIO buffer size · Issue #133521 · python/cpython
May 6, 2025 - https://stackoverflow.com/questions/26827055/python-how-to-get-bytesio-allocated-memory-length The length of the data stored in a BytesIO object can be accessed in several ways. The best way seems to be getbuffer().nbytes. But it's not d...
Author   python
🌐
Reddit
reddit.com › r/learnpython › vs
r/learnpython on Reddit: <class 'bytes'> vs <class '_io.BytesIO'>
December 1, 2022 -

Hello all,

I'm trying to wrap my head around the practical differences between:

<class 'bytes'> and <class '_io.BytesIO'>.

I read through the documentation:

https://docs.python.org/3/library/io.html?highlight=bytesio#binary-i-o

Binary I/O (also called buffered I/O) expects bytes-like objects and produces bytes objects. No encoding, decoding, or newline translation is performed. This category of streams can be used for all kinds of non-text data, and also when manual control over the handling of text data is desired.

It provides some examples:

The easiest way to create a binary stream is with open() with 'b' in the mode string:

and

f = io.BytesIO(b"some initial binary data: \x00\x01")

So I read all this, but so what? Why would you use the io.BytesIO data type over a standard bytes data type?

EDIT: Let me provide some additional context that I just discovered after reading the documentation on lxml.

https://lxml.de/parsing.html#parsing-html

I'm using the requests object and parsing the results with lxml. Here is the example code:

from io import BytesIO
from lxml import etree
#* etree - https://lxml.de/parsing.html
#? etree stands for element tree

import requests

#? Need to know concepts
#?  What are bytes
#?  HTTP status codes
#?  HTTP methods (GET. POST, PUT, DELETE)
#?  bytes - https://docs.python.org/3/library/stdtypes.html?highlight=bytes#bytes-objects

url = 'http://localhost'
#! The URL https://nostarch.com/ doesn't seem to work

resp = requests.get(url=url)
html_bytes = resp.content
parser = etree.HTMLParser()
content = etree.parse(BytesIO(html_bytes), parser=parser)

print(type(html_bytes))
print(type(BytesIO(html_bytes)))

for link in content.findall('//a'):
    print(f"{link.get('href')} -> {link.text}")

Kind regards

🌐
GitHub
github.com › PyFilesystem › pyfilesystem2 › issues › 402
How to get a path pointing to io.BytesIO? · Issue #402 · PyFilesystem/pyfilesystem2
June 3, 2020 - I am finding lot of librarys that use the filename for opening the file. However, in my Flask app I would like to get a filename that points to IO.BytesIO without the need o using temporal files
Author   PyFilesystem
🌐
GitHub
github.com › pyinvoke › invoke › issues › 853
Python io.BytesIO binary stream as {out,err}_stream container · Issue #853 · pyinvoke/invoke
April 4, 2022 - AFAIK io.BytesIO() is not a 'str' object, it's a binary stream using an in-memory bytes buffer.
Author   pyinvoke
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-stringio-and-bytesio-compared-with-open
Python Stringio and Bytesio Compared With Open() - GeeksforGeeks
July 23, 2025 - StringIO and BytesIO are classes from the io module that provide file-like objects in memory. They act like virtual files, but instead of storing data on an Operating system disk, they store it in memory as strings (StringIO) or bytes (BytesIO).
🌐
Simon Willison
simonwillison.net › 2025 › Jan › 31 › save-memory-with-bytesio
The surprising way to save memory with BytesIO
January 31, 2025 - The surprising way to save memory with BytesIO (via) Itamar Turner-Trauring explains that if you have a BytesIO object in Python calling .read() on it will create a full copy of that object, doubling the amount of memory used - but calling .getvalue() returns a bytes object that uses no additional memory, instead using copy-on-write.
🌐
Requests
requests.readthedocs.io › en › latest › user › quickstart
Quickstart — Requests 2.34.2 documentation
May 18, 2026 - >>> from PIL import Image >>> from io import BytesIO >>> i = Image.open(BytesIO(r.content))
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_excel.html
pandas.read_excel — pandas 3.0.4 documentation
Read an Excel file into a DataFrame · Supports xls, xlsx, xlsm, xlsb, odf, ods and odt file extensions read from a local filesystem or URL. Supports an option to read a single sheet or a list of sheets
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-from-_io-bytesio-to-a-bytes-like-object-in-python
Convert from '_Io.Bytesio' to a Bytes-Like Object in Python - GeeksforGeeks
July 23, 2025 - In Python, converting from _io.BytesIO to a bytes-like object involves handling binary data stored in a BytesIO object. This transformation is commonly needed when working with I/O operations, allowing seamless access to the raw byte representation contained within the BytesIO buffer.
🌐
TechOverflow
techoverflow.net › 2019 › 07 › 24 › how-to-write-bytesio-content-to-file-in-python
How to write BytesIO content to file in Python | TechOverflow
July 23, 2019 - """ with open(filename, "wb") as outfile: # Copy the BytesIO stream to the output file outfile.write(bytesio.getbuffer()) write_bytesio_to_file("out.txt", myio)