ahupp / python-magic

A python wrapper for libmagic
Other
2.6k stars 281 forks source link

magic.from_file() working, but magic.from_buffer() raising UnicodeDecodeError #226

Closed gabn88 closed 3 years ago

gabn88 commented 3 years ago

If I do magic.from_file(filepath, mime=True) I get the correct MIMEtype, however if I first open the file:

with open(filepath) as file:
    magic.from_buffer(file.read(), mime=True)

I get:

  File "/usr/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 10: invalid start byte.

This means I would have to open the file twice as I need it later on. Is there something I'm doing wrong or it is a bug?

ahupp commented 3 years ago

I'm guessing you figured this out, but you'll want open(..., mode='b') here.

gabn88 commented 3 years ago

Thanks! I didn't find that answer, was struggling through the encoding documentation.