Convert file into BytesIO object using python

user2961127

I have a file and want to convert it into BytesIO object so that it can be stored in database's varbinary column.

Please can anyone help me convert it using python.

Below is my code:

f = open(filepath, "rb")
print(f.read())

myBytesIO = io.BytesIO(f)
myBytesIO.seek(0)
print(type(myBytesIO))
Stephan Klein

Opening a file with open and mode read-binary already gives you a Binary I/O object.

Documentation:

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

f = open("myfile.jpg", "rb")

So in normal circumstances, you'd be fine just passing the file handle wherever you need to supply it. If you really want/need to get a BytesIO instance, just pass the bytes you've read from the file when creating your BytesIO instance like so:

with open(filepath, 'rb') as fh:
    buf = BytesIO(fh.read())

This has the disadvantage of loading the entire file into memory, which might be avoidable if the code you're passing the instance to is smart enough to stream the file without keeping it in memory. Note that the example uses open as a context manager that will reliably close the file, even in case of errors.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i convert from '_io.BytesIO' to a bytes-like object in python3.6?

Writing a BytesIO object to a file, 'efficiently'

Tensorflow Convert pb file to TFLITE using python

Convert BytesIO into File

Convert from '_io.BytesIO' to a bytes-like object in python3.6?

BytesIO object to image

Convert dictionary of lists to a CSV file using Python

Bokeh - Pandas not able to read bytesIO object of excel file from JS

csv file convert to io.BytesIO object, then stream to blob storage ,meets value type error:a bytes-like object is required, not '_io.TextIOWrapper'

convert json format file to tsv using python

Why I have to convert bytes from BytesIO then convert back to BytesIO so it can be read as PDF file response?

BytesIO like file object

Convert xml file to Java object using Jaxb

Unable to convert JSON file to CSV using Python

Using bash convert a JSON file to JSON Object

What is correct way to load json file stored in the BytesIO object?

Convert .OBJ file to STL using FreeCAD python

How do I convert a python object to (json) nested dict using the json module, without making a file-like object?

convert JSON to CSV file using python

Convert a text file into table using Python

convert JSON into CSV and merge object using Python

How can I convert a CSV file to a JSON file with a nested json object using Python?

Python error: PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x1144e9860>

TypeError: a bytes-like object is required, not 'str' Using BytesIO

convert .txt file to .csv file using python

Convert png/jpg to word file using python

convert html file to BytesIO then pass as Flask variable

download an image file from S3 and send as email in python using BytesIO and MIMEApplication results to zero bytes

Assertion error for BytesIO object in Python unittest