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, BufferedReader, and BufferedRWPair buffer raw binary streams that are writable, readable, and both readable and writable, respectively. BufferedRandom provides a buffered interface to seekable streams. Another BufferedIOBase subclass, BytesIO, is a stream of in-memory bytes.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-io-bytesio-stringio
Python io.BytesIO and io.StringIO: Memory File Guide | DigitalOcean
August 3, 2022 - There are many ways in which we can use the io module to perform stream and buffer operations in Python. We will demonstrate a lot of examples here to prove the point. Let’s get started. Just like what we do with variables, data can be kept as bytes in an in-memory buffer when we use the io module’s Byte IO operations. Here is a sample program to demonstrate this: import io stream_str = io.BytesIO(b"JournalDev Python: \x00\x01") print(stream_str.getvalue())
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › io.BytesIO.read.html
io.BytesIO.read — Python Standard Library
io.BytesIO.read · View page source · BytesIO.read([size]) → read at most size bytes, returned as a string.¶ · If the size argument is negative, read until EOF is reached.
🌐
Python⇒Speed
pythonspeed.com › articles › bytesio-reduce-memory-usage
The surprising way to save memory with BytesIO
February 27, 2025 - How does this work? BytesIO is using copy-on-write. Internally, it keeps a reference to the new bytes object returned from getvalue(). So long as you don’t write to the BytesIO, any reads can happen off the same memory.
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › io.BytesIO.html
io.BytesIO — Python Standard Library
class io.BytesIO¶ · Create a buffered I/O implementation using an in-memory bytes buffer, ready for reading and writing.
🌐
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 ... data (bytes) instead of text. It lets you perform operations on these bytes, such as reading and writing, as if you were interacting with a regular file....
Find elsewhere
🌐
Andrew Wheeler
andrewpwheeler.com › 2022 › 11 › 02 › using-io-objects-in-python-to-read-data
Using IO objects in python to read data | Andrew Wheeler
November 2, 2022 - # Example 2, grabbing zipped contents import zipfile from io import BytesIO census_url = 'https://www2.census.gov/programs-surveys/acs/summary_file/2019/data/2019_5yr_Summary_FileTemplates.zip' req = requests.get(census_url) # Can use BytesIO for this content zf = zipfile.ZipFile(BytesIO(req.content)) The zipfile library would be equivalent to reading/extracting a zipfile already on disk.
🌐
Runebook.dev
runebook.dev › en › docs › python › library › io › io.BytesIO
python - BytesIO Gotchas and Workarounds: Fixing Empty Reads and TypeErrors
import io buffer = io.BytesIO(b"Data to read twice.") # First read (cursor moves to end) first_read = buffer.read() print(f"1st read: {first_read}") # Second read attempt (cursor is still at the end) second_read = buffer.read() print(f"2nd read (empty): {second_read}") # Fix: Move the cursor back to the beginning buffer.seek(0) # Third read (works now!) third_read = buffer.read() print(f"3rd read (fixed): {third_read}") io.BytesIO strictly handles bytes. You cannot directly write regular Python strings (str) to it.
🌐
Code-learner
code-learner.com › home › python stringio and bytesio example
Python StringIO And BytesIO Example ·
March 21, 2021 - Python StringIO and BytesIO are methods that manipulate string and bytes data in memory, this makes memory data manipulation use the consistent API as read and write files. StringIO is used to operate string data, and if you want to manipulate binary data, you need to use BytesIO.
🌐
Learning Machine
gallon.me › using-the-bytesio-class-in-python.html
Using the BytesIO Class in Python - Learning Machine
February 14, 2025 - ```python # Writing binary data to a file: with open('output.bin', 'wb') as f: f.write(b'\x00\x01\x02') # Reading binary data from a file: with open('output.bin', 'rb') as f: data = f.read() print(data) # Output: b'\x00\x01\x02' ``` In-memory File-like Object: BytesIO acts like a file that exists in memory.
🌐
GitHub
github.com › python › cpython › blob › main › Modules › _io › bytesio.c
cpython/Modules/_io/bytesio.c at main · python/cpython
BytesIO objects are not connected to a TTY-like device. ... Current file position, an integer. ... Read at most size bytes, returned as a bytes object.
Author   python
🌐
Python Assets
pythonassets.com › posts › what-is-io-bytesio-useful-for
What Is `io.BytesIO` Useful For? | Python Assets
July 19, 2024 - This means you can read from and write to it just like a file, but without creating any actual files on disk. In Python, file-like objects are objects that implement methods like read(), write(), and seek(), allowing you to interact with data ...
🌐
ProgramCreek
programcreek.com › python › example › 1734 › io.BytesIO
Python Examples of io.BytesIO
:rtype : ``list`` ''' decoder = avro.io.BinaryDecoder(io.BytesIO(rawbytes)) reader = avro.io.DatumReader(avro.schema.parse(AVSC)) try: return reader.read(decoder)[list.__name__] except Exception as exc: logging.getLogger('SPOT.INGEST.COMMON.SERIALIZER')\ .error('[{0}] {1}'.format(exc.__class__.__name__, exc.message)) return []
🌐
AskPython
askpython.com › home › python io module: the complete practical reference
Python IO Module: The Complete Practical Reference - AskPython
February 16, 2023 - In this article, we learned about using the Python IO module, and it’s two main classes – io.BytesIO and io.StringIO for reading and writing byte and string data onto a buffer.
🌐
Python.org
discuss.python.org › ideas
Add search methods from bytes/bytearray objects to io.BytesIO - Ideas - Discussions on Python.org
November 5, 2022 - I worked heavily with BytesIO objects in one of my applications, and recently I thought that it would be useful to be able to look through the object for specific values. For this I did something like: file.read().find(text), which I think is creating a copy of the entire bytes object.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-stringio-and-bytesio-compared-with-open
Python Stringio and Bytesio Compared With Open() - GeeksforGeeks
July 23, 2025 - In this example, below code shows how to use BytesIO to handle binary data in memory, first by writing binary data to a BytesIO object, then by reading the data using open() to write it to a temporary binary file (temp_file.bin).