Skip to main content

content-types 🗃️🔎

A Python library to map file extensions to MIME types. It also provides a CLI for quick lookups right from your terminal. If no known mapping is found, the tool returns application/octet-stream.

Unlike other libraries, this one does not try to access the file or parse the bytes of the file or stream. It just looks at the extension which is valuable when you don't have access to the file directly. For example, you know the filename but it is stored in s3 and you don't want to download it just to fully inspect the file.

Why not just use Python's built-in mimetypes? Or the excellent python-magic package? See below.

Installation

uv pip install content-types

Usage

import content_types

# Forward lookup: filename -> MIME type
the_type = content_types.get_content_type("example.jpg")
print(the_type)  # "image/jpeg"

# For very common files, you have shortcuts:
print(f'Content-Type for webp is {content_types.webp}.') 
# Content-Type for webp is image/webp.

CLI

To use the library as a CLI tool, just install it with uv or pipx.

uv tool install content-types

Now it will be available machine-wide.

content-types example.jpg

# Outputs image/jpeg

More correct than Python's mimetypes

When I first learned about Python's mimetypes module, I thought it was exactly what I need. However, it doesn't have all the MIME types. And, it recommends deprecated, out-of-date answers for very obvious types.

For example, mimetypes has .xml as text/xml where it should be application/xml (see MDN).

And mimetypes is missing important types such as:

  • .m4v -> video/mp4
  • .tgz -> application/gzip
  • .flac -> audio/flac
  • .epub -> application/epub+zip
  • ...

Here is a full comparison found by running samples/compare_to_builtin.py:

There are 5 types where mimetypes and content-types disagree

mimetypes: .wav audio/x-wav, content-types: .wav audio/wav
mimetypes: .obj application/octet-stream, content-types: .obj model/obj
mimetypes: .xml text/xml, content-types: .xml application/xml
mimetypes: .exe application/octet-stream, content-types: .exe application/x-msdownload
mimetypes: .dll application/octet-stream, content-types: .dll application/x-msdownload

There are 0 types in mimetypes that are not in content-types

There are 31 types in content-types that are not in mimetypes

.docx -> application/vnd.openxmlformats-officedocument.wordprocessingml.document
.m4v  -> video/mp4
.odp  -> application/vnd.oasis.opendocument.presentation
.deb  -> application/x-debian-package
.glb  -> model/gltf-binary
.php  -> application/x-httpd-php
.xlsx -> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.woff -> font/woff
.tgz  -> application/gzip
.ogg  -> audio/ogg
.odt  -> application/vnd.oasis.opendocument.text
.wmv  -> video/x-ms-wmv
.stl  -> model/stl
.ttf  -> font/ttf
.flac -> audio/flac
.rar  -> application/vnd.rar
.odg  -> application/vnd.oasis.opendocument.graphics
.ods  -> application/vnd.oasis.opendocument.spreadsheet
.weba -> audio/webm
.gltf -> model/gltf+json
.epub -> application/epub+zip
.m4a  -> audio/mp4
.map  -> application/json
.pptx -> application/vnd.openxmlformats-officedocument.presentationml.presentation
.woff2 -> font/woff2
.otf  -> font/otf
.gz   -> application/gzip
.rpm  -> application/x-rpm
.7z   -> application/x-7z-compressed
.ogv  -> video/ogg
.apk  -> application/vnd.android.package-archive

Contributing

Contributions are welcome! Check out the GitHub repo for more details on how to get involved.

Metadata

Release files for content-types 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for content-types 0.2.3
File Size Uploaded Provenance Actions
content_types-0.2.3.tar.gz 8.0 kB Without provenance Details for content_types-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for content-types 0.2.3
File Interpreter ABI Platform Actions
content_types-0.2.3-py3-none-any.whl Python 3 none any Details for content_types-0.2.3-py3-none-any.whl

Total release size: 15.0 kB

Release files / content_types-0.2.3.tar.gz

Download URL content_types-0.2.3.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4ac8c4dc7e8b9a8f328e3b5e0a3525a5a98757b9a3aa2f94b3de4aa6df182778
BLAKE2b-256 checksum
How to use checksums
00d6f35272839b2b4cbe9754bdc4e812f2f2438a439f38ae1d8058d97df74467
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.25

Release files / content_types-0.2.3-py3-none-any.whl

Download URL content_types-0.2.3-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80795cd85cecb9ce45b3073b73a1effe141c1335abf362d315053bd39abc9e2b
BLAKE2b-256 checksum
How to use checksums
8e276b6005a4415e848f1f6692f4a6c57cc5e1bb0dc38b0fc4e9da86e5f8d7d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.25

Security for content-types 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page