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 Overflowpython - Writing then reading in-memory bytes (BytesIO) gives a blank result - Stack Overflow
Document BytesIO buffer size
<class 'bytes'> vs <class '_io.BytesIO'>
How to get a path pointing to io.BytesIO?
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
You need to seek back to the beginning of the file after writing the initial in memory file...
myio.seek(0)
How about we write and read gzip content in the same context like this?
#!/usr/bin/env python
from io import BytesIO
import gzip
content = b"does it work"
# write bytes to zip file in memory
gzipped_content = None
with BytesIO() as myio:
with gzip.GzipFile(fileobj=myio, mode='wb') as g:
g.write(content)
gzipped_content = myio.getvalue()
print(gzipped_content)
print(content == gzip.decompress(gzipped_content))
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