Open In App

Find the Mime Type of a File in Python

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Determining the MIME (Multipurpose Internet Mail Extensions) type of a file is essential when working with various file formats. Python provides several libraries and methods to efficiently check the MIME type of a file. In this article, we’ll explore different approaches to find the mime type of a file in Python.

Mime Type of a File in Python

Below are some of the ways by which we can find the mime type of a file in Python:

  1. Using the mimetypes module
  2. Using the python-magic Library
  3. Using the imghdr Module
  4. Using the os Module

Using the mimetypes Module

In this example, the Python code utilizes the mimetypes module to determine the MIME type of the file ‘output.txt’ using the guess_type function. The resulting MIME type is then printed to the console.

Python3




import mimetypes
 
# Get MIME type using guess_type
file_path = 'output.txt'
mime_type, encoding = mimetypes.guess_type(file_path)
print("MIME Type:", mime_type)


Output:

Screenshot-2024-02-20-200020

Using the Python-Magic Library

In this example, the Python code employs the python-magic library to create a magic object, allowing the determination of the MIME type for the file ‘output.txt’ using the from_file method. The resulting MIME type is then printed to the console.

Python3




import magic
 
# Create a magic object
mime = magic.Magic()
 
# Get MIME type using from_file
file_path = 'output.txt'
mime_type = mime.from_file(file_path)
print(f'MIME Type: {mime_type}')


Output:

Screenshot-2024-02-20-201051

Using the imghdr Module for Images

The imghdr module is specifically used for determining the MIME type of images.The what function returns the MIME type of the specified image file. In the above code On passing the image path to the what() method, It returned the mime type of the image file

Python3




import imghdr
 
# Get MIME type for images using what
image_path = 'image005.png'
mime_type = imghdr.what(image_path)
print(f'MIME Type: {mime_type}')


Output:

Screenshot-2024-02-20-201315

Using the os Module

In this example, the Python code uses os module and defines a function get_mime_type that extracts the file extension from a given file path and maps it to a corresponding MIME type using a predefined dictionary. The function returns the determined MIME type, and an example usage demonstrates its application on the file ‘output.txt’, with the result printed to the console.

Python3




import os
 
def get_mime_type(file_path):
    # Extract the file extension from the file path
    _, file_extension = os.path.splitext(file_path)
 
    # Map common file extensions to MIME types
    mime_types = {
        '.txt': 'text/plain',
        '.pdf': 'application/pdf',
        '.jpg': 'image/jpeg',
        '.png': 'image/png',
        # Add more file extensions and MIME types as needed
    }
 
    # Return the corresponding MIME type.
    return mime_types.get(file_extension.lower(), 'application/octet-stream')
 
# Example usage
file_path = 'output.txt'
mime_type = get_mime_type(file_path)
 
print(f'MIME type of {file_path}: {mime_type}')


Output:

Screenshot-2024-02-20-201512



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads