File Mime types in Django

import mimetypesfile_mime = mimetypes.guess_type('a_file.jpg')>> ('image/jpeg', None)
import mimetypesfile_mime = mimetypes.guess_type('sneaky_pdf_file.jpg')>> ('image/jpeg', None)
pip3 install python-magic
def file_path_mime(file_path):
mime = magic.from_file(file_path, mime=True)
return mime


def check_in_memory_mime(in_memory_file):
mime = magic.from_buffer(in_memory_file.read(), mime=True)
return mime
file_path_mime('/path/to/sneaky_pdf_file.jpg')>> 'application/pdf'file_path_mime('path/to/a_file.jpg'):>> 'image/jpg'

--

--

Pub Tech developer for Birkbeck CTP

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andy Byers

Andy Byers

Pub Tech developer for Birkbeck CTP