Facebooktwittergoogle_plusredditpinterestlinkedinmail

Get file MIME type in Python

You can get mime type in python by using python-magic library. You can install python-magic using PIP.

$ pip install python-magic

To find mime type, import magic library and pass file path into magic.from_file()

>>> import magic
>>> magic.from_file("path/to/file.pdf", mime=True)
'application/pdf'

If you face error “AttributeError: ‘module’ object has no attribute ‘from_file’” , then here is work around for this.

>>> import magic
>>> m = magic.open(magic.MAGIC_MIME)
>>> m.load()
>>> m.file("path/to/file.pdf")
Get File MIME Type in Python Roopendra PythonScripting,,
Get file MIME type in Python You can get mime type in python by using python-magic library. You can install python-magic using PIP. $ pip install python-magic To find mime type, import magic library and pass file path into magic.from_file() >>> import magic >>> magic.from_file('path/to/file.pdf', mime=True) 'application/pdf' If you face error 'AttributeError: 'module' object has no...
<h2>Get file MIME type in Python</h2> You can get mime type in python by using <strong>python-magic</strong> library. You can install <strong>python-magic</strong> using PIP. <pre>$ pip install python-magic</pre> To find mime type, import magic library and pass file path into <strong>magic.from_file()</strong> <pre>>>> import magic >>> magic.from_file("path/to/file.pdf", mime=True) 'application/pdf'</pre> If you face error "<strong>AttributeError: 'module' object has no attribute 'from_file'</strong>" , then here is work around for this. <pre>>>> import magic >>> m = magic.open(magic.MAGIC_MIME) >>> m.load() >>> m.file("path/to/file.pdf")</pre>
(Visited 2,472 times, 125 visits today)
Facebooktwittergoogle_pluslinkedinrss