Skip to content

Instantly share code, notes, and snippets.

@cbwar
Last active September 22, 2022 07:52
Show Gist options
  • Save cbwar/0aea77973855c9cb07a74359e61f8e4d to your computer and use it in GitHub Desktop.
Save cbwar/0aea77973855c9cb07a74359e61f8e4d to your computer and use it in GitHub Desktop.
Python: Get mime type from file
# https://stackoverflow.com/questions/43580/how-to-find-the-mime-type-of-a-file-in-python
import magic
mime = magic.Magic(mime=True)
mime.from_file("testdata/test.pdf") # 'application/pdf'
@Myzel394
Copy link

Myzel394 commented Oct 3, 2020

Handy function but DON'T USE THIS TO DETERMINE THE TRUE MIMETYPE OF A FILE!!! You can easily rename a file extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment