Search code examples
pythoniopicklejoblibbytesio

Read model as bytes without saving in location in python?


Python, I have saved my model as joblib file in a location, the I open the file in 'rb' read bytes, is it possible to convert straight to bytes instead of saving in a file,

import joblib
joblib.dump(model, 'model.joblib')
#Read as bytes
model_bytes = open('C:/Models/model.joblib','rb').read()
model_bytes
#This outputs like 
b'\x80\x03csklearn.ensemble.forest\nRandomForestClassifier\nq\x00)\x81q\x01}q\x...…..

Here I don't want to save in a location, so I tried with tempfile, but this will not work I knew, is there any other options

import tempfile
bytes_model = tempfile.TemporaryFile()
bytes_model.read(model)

#Also bytes function doesn't work
bytes_model = bytes(model)

I don't need a file to be created, so that I don't have to access it, Is it possible to read the model variable as bytes?


Solution

  • You should be able to use BytesIO for this if joblib.dump() doesn't complain.

    Something like this may work for you:

    from io import BytesIO
    import joblib
    
    bytes_container = BytesIO()
    joblib.dump(model, bytes_container)
    bytes_container.seek(0)  # update to enable reading
    
    bytes_model = bytes_container.read()