36

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))
4
  • 1
    BytesIO simulates a file stream from a buffer. if you just want the byte data, data = f.read() is all you need. Commented Dec 16, 2019 at 22:24
  • That doesn't make any sense, if what you are working with accepts a BytesIO object then you should just be able to pass f, but note, don't read from it first. Commented Dec 16, 2019 at 22:26
  • 1
    Use a context manager to handle the file! Commented Dec 16, 2019 at 22:52
  • it does make sense, there are some libraries which accept in memory objects only and not file objects, additionally the only answer suggested below reads the entire file into memory Commented Apr 26, 2024 at 10:46

1 Answer 1

74

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:

from io import BytesIO

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I, for one, certainly hope the code I am passing the bytesIO to is not "smart" enough to stream the file from disk, as I am trying to keep the harddrive busy loading and saving, rather than waiting for loaded files to be processed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.