Save a file with arbitrary format uploaded using st.file_uploader()

Hello,

My app is running locally, I try to just save a file with arbitrary format locally (but it could be on a server, if we assume the saving destination is an arbitrary parameter) uploaded using st.file_uploader().

I am trying to use BytesIO but I am getting an error :

b = BytesIO(my_upload.getvalue())
  with open("test.npy", "w+") as f:
      f.write(b)

TypeError: write() argument must be str, not _io.BytesIO

Is there a simple - conventional way to just store an UploadFile object value without modifying it ?

Thank you

The my_upload.getvalue() is already in bytes. And instead of β€œw+” use β€œwb” for binary writing.

So try this:

b = my_upload.getvalue()
with open("test.npy", "wb") as f:
    f.write(b)
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.