3

The library I am using doesn't support a BytesIO object or binary data. Instead, it expects a file path in disk to be opened as binary.

I want to pass my raw data or even a BytesIO object as a file path argument, without saving it to disk (something like faking it's a file in disk to be opened by the library).

It it possible?

What I have so far (saving it to disk):

import os

main_dir = os.path.dirname(__file__)
output = os.path.join(main_dir, 'output') # this is my file in disk

# raw_data is by data source (binary / hexadecimal)
with open(output, 'wb') as f:
        f.write(raw_data)
Julio S.
  • 944
  • 1
  • 12
  • 26
  • 1
    You could use a named pipe. – Iguananaut Jul 28 '21 at 17:14
  • @Iguananaut, could you explain better? – Julio S. Aug 04 '21 at 19:07
  • @Iguananaut I could make it work by using a pipe. The issue is that when the data exceeds 4096 Bytes, it freezes. Do you know any workaround for this? – Julio S. Aug 04 '21 at 21:50
  • I too need help with this problem. I am using the built-in library mimetypes to guess file's mime-type. The file is received from api in base64 format and I have stored it in BytesIO object. I do not want disk i/o or storage utilization. So how can i pass the BytesIO object directly to mimetypes.guess_type() function? – Ajeet Mishra Jul 28 '22 at 10:25

0 Answers0