# 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
```bash
uv pip install content-types
```
## Usage
```python
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**.
```bash
uv tool install content-types
```
Now it will be available machine-wide.
```bash
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](https://developer.mozilla.org/en-US/docs/Web/HTTP/MIME_types/Common_types)).
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`:
```text
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](https://github.com/mikeckennedy/content-types)
for more details on how to get involved.
Raw data
{
"_id": null,
"home_page": null,
"name": "content-types",
"maintainer": null,
"docs_url": null,
"requires_python": "<=3.14,>=3.10",
"maintainer_email": null,
"keywords": "content-type, file extensions, mapping, mime",
"author": null,
"author_email": "Michael Kennedy <mikeckennedy@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/00/d6/f35272839b2b4cbe9754bdc4e812f2f2438a439f38ae1d8058d97df74467/content_types-0.2.3.tar.gz",
"platform": null,
"description": "\n# content-types \ud83d\uddc3\ufe0f\ud83d\udd0e\n\nA Python library to map file extensions to MIME types. \nIt also provides a CLI for quick lookups right from your terminal.\nIf no known mapping is found, the tool returns `application/octet-stream`.\n\nUnlike other libraries, this one does **not** try to access the file \nor parse the bytes of the file or stream. It just looks at the extension\nwhich is valuable when you don't have access to the file directly.\nFor example, you know the filename but it is stored in s3 and you don't want\nto download it just to fully inspect the file.\n\nWhy not just use Python's built-in `mimetypes`? Or the excellent `python-magic` package? See below.\n\n## Installation\n\n```bash\nuv pip install content-types\n```\n\n## Usage\n\n```python\nimport content_types\n\n# Forward lookup: filename -> MIME type\nthe_type = content_types.get_content_type(\"example.jpg\")\nprint(the_type) # \"image/jpeg\"\n\n# For very common files, you have shortcuts:\nprint(f'Content-Type for webp is {content_types.webp}.') \n# Content-Type for webp is image/webp.\n```\n\n## CLI\n\nTo use the library as a CLI tool, just install it with **uv** or **pipx**. \n\n```bash\nuv tool install content-types\n```\n\nNow it will be available machine-wide.\n\n```bash\ncontent-types example.jpg\n\n# Outputs image/jpeg\n```\n\n## More correct than Python's `mimetypes`\n\nWhen I first learned about Python's mimetypes module, I thought it was exactly what I need. However, \nit doesn't have all the MIME types. And, it recommends deprecated, out-of-date answers for very obvious types.\n\nFor example, mimetypes has `.xml` as text/xml where it should be `application/xml` \n(see [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/MIME_types/Common_types)).\n\nAnd mimetypes is missing important types such as:\n\n- .m4v -> video/mp4\n- .tgz -> application/gzip\n- .flac -> audio/flac\n- .epub -> application/epub+zip\n- ...\n\nHere is a full comparison found by running `samples/compare_to_builtin.py`:\n\n```text\nThere are 5 types where mimetypes and content-types disagree\n\nmimetypes: .wav audio/x-wav, content-types: .wav audio/wav\nmimetypes: .obj application/octet-stream, content-types: .obj model/obj\nmimetypes: .xml text/xml, content-types: .xml application/xml\nmimetypes: .exe application/octet-stream, content-types: .exe application/x-msdownload\nmimetypes: .dll application/octet-stream, content-types: .dll application/x-msdownload\n\nThere are 0 types in mimetypes that are not in content-types\n\nThere are 31 types in content-types that are not in mimetypes\n\n.docx -> application/vnd.openxmlformats-officedocument.wordprocessingml.document\n.m4v -> video/mp4\n.odp -> application/vnd.oasis.opendocument.presentation\n.deb -> application/x-debian-package\n.glb -> model/gltf-binary\n.php -> application/x-httpd-php\n.xlsx -> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n.woff -> font/woff\n.tgz -> application/gzip\n.ogg -> audio/ogg\n.odt -> application/vnd.oasis.opendocument.text\n.wmv -> video/x-ms-wmv\n.stl -> model/stl\n.ttf -> font/ttf\n.flac -> audio/flac\n.rar -> application/vnd.rar\n.odg -> application/vnd.oasis.opendocument.graphics\n.ods -> application/vnd.oasis.opendocument.spreadsheet\n.weba -> audio/webm\n.gltf -> model/gltf+json\n.epub -> application/epub+zip\n.m4a -> audio/mp4\n.map -> application/json\n.pptx -> application/vnd.openxmlformats-officedocument.presentationml.presentation\n.woff2 -> font/woff2\n.otf -> font/otf\n.gz -> application/gzip\n.rpm -> application/x-rpm\n.7z -> application/x-7z-compressed\n.ogv -> video/ogg\n.apk -> application/vnd.android.package-archive\n```\n\n\n## Contributing\n\nContributions are welcome! Check out [the GitHub repo](https://github.com/mikeckennedy/content-types) \nfor more details on how to get involved.\n",
"bugtrack_url": null,
"license": null,
"summary": "A library to map file extensions to content types and vice versa.",
"version": "0.2.3",
"project_urls": {
"Bug Reports": "https://github.com/mikeckennedy/content-types/issues",
"Homepage": "https://github.com/mikeckennedy/content-types",
"Source": "https://github.com/mikeckennedy/content-types"
},
"split_keywords": [
"content-type",
" file extensions",
" mapping",
" mime"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "8e276b6005a4415e848f1f6692f4a6c57cc5e1bb0dc38b0fc4e9da86e5f8d7d2",
"md5": "61b267d890cfcfa3986f5a41c1c644b9",
"sha256": "80795cd85cecb9ce45b3073b73a1effe141c1335abf362d315053bd39abc9e2b"
},
"downloads": -1,
"filename": "content_types-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "61b267d890cfcfa3986f5a41c1c644b9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<=3.14,>=3.10",
"size": 6909,
"upload_time": "2025-02-01T15:40:36",
"upload_time_iso_8601": "2025-02-01T15:40:36.077027Z",
"url": "https://files.pythonhosted.org/packages/8e/27/6b6005a4415e848f1f6692f4a6c57cc5e1bb0dc38b0fc4e9da86e5f8d7d2/content_types-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "00d6f35272839b2b4cbe9754bdc4e812f2f2438a439f38ae1d8058d97df74467",
"md5": "a85bee41fd02f654074aacce9575e1be",
"sha256": "4ac8c4dc7e8b9a8f328e3b5e0a3525a5a98757b9a3aa2f94b3de4aa6df182778"
},
"downloads": -1,
"filename": "content_types-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "a85bee41fd02f654074aacce9575e1be",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<=3.14,>=3.10",
"size": 8044,
"upload_time": "2025-02-01T15:40:37",
"upload_time_iso_8601": "2025-02-01T15:40:37.083343Z",
"url": "https://files.pythonhosted.org/packages/00/d6/f35272839b2b4cbe9754bdc4e812f2f2438a439f38ae1d8058d97df74467/content_types-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-01 15:40:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mikeckennedy",
"github_project": "content-types",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "content-types"
}