Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to get file mimetype in Python?

pip install python-magic

Now get file mime type like below:

>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'


If you do not provide mime=True:

>>> magic.from_file("testdata/test.pdf")
'PDF document, version 1.2'


If you are on OS X you may get the below error:

ImportError: failed to find libmagic.  Check your installation


To solve the problem install libmagic using brew:

brew install libmagic


Or using macport:

port install file

Read more here:
- https://github.com/ahupp/python-magic

#python #python_magic #magic #mimetype #mime #libmagic