Get the MIME type in Python and determine the file format

How to get the MIME type of a file in Python. By doing this, you can change the behavior by judging the format of the input file.

Get

import mimetypes
mime = mimetypes.guess_type(File path)
print(mime) # => (MIME type,Encode format)

That's it. In the encoding format part, if it is a file compressed with gzip, some value will appear, but basically it will be None.

Behavior changes depending on the file format

As a simple example, let's say you only accept Markdown files. If any other file is entered, an error message will be displayed.

markdown.py


import mimetypes
import sys

input = sys.argv[1]
mime = mimetypes.guess_type(input)

if mime[0] = "text/markdown":
    print("You're a Markdown file!")
else:
    print("{mime}Type of file is over there!".format(mime=mime[0]))

When you run ...

$ python3 markdown.py hoge.md
You're a Markdown file!
$ python3 markdown.py fuga.jpg
image/jpeg type files are sick!

Recommended Posts

Get the MIME type in Python and determine the file format
Determine the date and time format in Python and convert to Unixtime
Get the MIME Type
Format the Git log and get the committed file name in csv format
Download the file in Python
Get the result in dict format with Python psycopg2
I want to get the file name, line number, and function name in Python 3.4
Save the binary file in Python
Get the desktop path in Python
Get the script path in Python
Get the current date and time in Python, considering the time difference
Get the desktop path in Python
Get the host name in Python
python> Display 3 decimals in ".3f, .3f, .3f" format / Get 3 coordinate values in the range [-1: 1]
Get the formula in an excel file as a string in Python
Get the title and delivery date of Yahoo! News in Python
Get date and time in specified format
Write O_SYNC file in C and Python
Read the file line by line in Python
Read the file line by line in Python
[Python] Get the character code of the file
Get the EDINET code list in Python
[Python] Read the specified line in the file
format in python
[Python Kivy] How to get the file path by dragging and dropping
How to get the date and time difference in seconds with python
python> array> Determine the number and initialize> mylist = [idx for idx in range (10)] / mylist = [0 for idx in range (10)] >> mylist = [0] * 10
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
The file name was bad in Python and I was addicted to import
Get and convert the current time in the system local timezone with python
Open an Excel file in Python and color the map of Japan
Get the weather in Osaka via WebAPI (python)
File DL, byte value and delete in Python3
Get the caller of a function in Python
Get multiple maximum keys in Python dictionary type
About the difference between "==" and "is" in python
Get the X Window System window title in Python
How to get the files in the [Python] folder
[Python] Determine the type of iris with SVM
Get the update date of the Python memo file.
Create a Python image in Django without a dummy image file and test the image upload
Get the last element of the array by splitting the string in Python and PHP
File operations in Python
File processing in Python
Image format in Python
File operations in Python
Get date in Python
How to get the variable name itself in python
The simplest Python memo in Japan (classes and objects)
Receive the form in Python and do various things
Get the file name in a folder using glob
How to get the number of digits in Python
Automatically determine and process the encoding of the text file
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the numbers in the graph image with OCR
[python] Get the list of classes defined in the module
Determine the threshold using the P tile method in python
Carefully understand the exponential distribution and draw in Python
Read the csv file and display it in the browser
Plot and understand the multivariate normal distribution in Python
Carefully understand the Poisson distribution and draw in Python