Skip to main content

python-magic-file

Small Python module to aid developers in getting file extensions from files.

Purpose

The main motivation behind this package is to easily get the file extension from given file instead of trusting the arbitrary file extension in the filename, for example in a web application which accepts file uploads.

Usage

Some examples on how to use this module.

Get extension from file

from python_magic_file import MagicFile

file_path = 'path/to/file'

with open(file_path 'rb') as f:
    magic_file = MagicFile(f)
    extension = magic_file.get_extension()

Get extensions from file

from python_magic_file import MagicFile

file_path = 'path/to/file'

with open(file_path 'rb') as f:
    magic_file = MagicFile(f)
    extensions = magic_file.get_extensions()

Register unknown/non-standard mimetypes

There may be some cases where get_extension() emits a following warning:

UserWarning: File extension for mimetype "video/x-m4v" is None, consider adding an extension for this mimetype using MagicFile.

We have to register file extension using MagicFile.add_type_to_mimetypes_module for video/x-m4v so get_extension() returns the registered extension instead of None.

MagicFile.add_type_to_mimetypes_module simply just calls mimetypes.add_type.

from python_magic_file import MagicFile

# A dictionary of mimetype/extension pairs
new_types = {'video/x-m4v': '.m4v'}

for mimetype, extension in new_types.items():
    MagicFile.add_type_to_mimetypes_module(mimetype, extension)

with open('path/to/m4v-file.m4v', 'rb') as f:
    magic_file = MagicFile(f)
    extension = magic_file.get_extension() # .m4v

Get human readable name for file

from python_magic_file import MagicFile

file_path = 'path/to/file.txt'

with open(file_path 'rb') as f:
    magic_file = MagicFile(f)
    human_readable_name = magic_file.get_name() # ASCII text, with no line terminators

Usage with Flask

import os

from flask import Flask, request, abort
from python_magic_file import MagicFile

from werkzeug.utils import secure_filename
from werkzeug.security import safe_join

app = Flask(__name__)

# Allowed extensions for file uplaod
UPLOAD_ALLOWED_EXTENSIONS = ('.jpg', '.jpeg', '.png')

@app.post('/upload')
def upload_file():
    uploaded_file = request.files.get('file')

    if uploaded_file is None:
        abort(400)

    extension = MagicFile(uploaded_file.stream).get_extension()
    
    if extension not in UPLOAD_ALLOWED_EXTENSIONS:
        abort(400)

    filename, _ = os.path.splitext(secure_filename(uploaded_file.filename))

    save_path = safe_join(os.getcwd(), filename + extension)

    uploaded_file.save(save_path)

    return 'OK'

if __name__ == '__main__':
    app.run()

Metadata

Release files for python-magic-file 0.0.7

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

Source distribution (sdist)

Source distribution for python-magic-file 0.0.7
File Size Uploaded Provenance Actions
python_magic_file-0.0.7.tar.gz 4.1 kB Without provenance Details for python_magic_file-0.0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-magic-file 0.0.7
File Interpreter ABI Platform Actions
python_magic_file-0.0.7-py3-none-any.whl Python 3 none any Details for python_magic_file-0.0.7-py3-none-any.whl

Total release size: 9.1 kB

Release files / python_magic_file-0.0.7.tar.gz

Download URL python_magic_file-0.0.7.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
63c66fed3e5aefef446bc3ecb3241b881754259b2fa0ecc13c8d2f24cbbf5b04
BLAKE2b-256 checksum
How to use checksums
5b692c54fa330d3c9dad26c8e96c7cf13d458d7830c7162adceebea9eda62595
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / python_magic_file-0.0.7-py3-none-any.whl

Download URL python_magic_file-0.0.7-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
665145d4fb90ae86fae70a7ec9a6b55a33186cc89b4cd8886328ea275babc84a
BLAKE2b-256 checksum
How to use checksums
2a0b09794283427d234adea80145c06b0ec14e2ba7f403d496e56e8d52c9e793
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Security for python-magic-file 0.0.7

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