Skip to main content

Simple file magic. We try to get file's mimetype using 'file-magic', 'command file' and 'puremagic'. On linux we need system package 'file-libs' which mostly already installed. On MacOS we need system package 'libimage' which can be installed by 'brew install libmagic'. On windows we need file command which can be install by 'pacman -S file' within msys2. If system package missing, we try to get the file's mimetype using 'puremagic' which is write in pure python without any extra depends.

Project description

simplemagic

Simple file magic. We try to get file's mimetype using 'file-magic', 'command file' and 'puremagic'. On linux we need system package 'file-libs' which mostly already installed. On MacOS we need system package 'libimage' which can be installed by 'brew install libmagic'. On windows we need file command which can be install by 'pacman -S file' within msys2. If system package missing, we try to get the file's mimetype using 'puremagic' which is write in pure python without any extra depends.

Install

pip3 install simplemagic

System requirements

Linux

  • file-libs

Mostly it is installed already, and you can installed it with command:

yum install file-libs

MacOS

  • libmagic

You can installed it with command:

brew install libmagic

Windows

libmagic mostly not working on windows. Suggest you install msys2 on in system, and in msys2 you can install libmagic with command:

pacman -S file

Add msys2's bin path to your system's PATH env. We can call the external command file to get the mimetype of a file.

APIS

  • simplemagic.get_mimetype_by_stream
  • simplemagic.get_mimetype_by_filename
  • simplemagic.guess_all_extensions

You can read the source code to find other private apis which maybe you will need to reset the global settings or running env.

Examples

import simplemagic

ext = ".docx"
filename = "ok.docx"
mimetype = simplemagic.get_mimetype_by_filename(filename)
ok_exts = simplemagic.guess_all_extensions(mimetype)
if ext in ok_exts:
    print("the file content is match with the file suffix...")

Notice

Always upgrade your libmagic to the latest, old libmagic may get wrong answer.

Compatibility

  • test passed on python3.6, python3.7, python3.8, python3.9 and python3.10
  • test failed on python2.7, python3.3, python3.4, python3.5

Releases

v0.1.0

  • First release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simplemagic-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

simplemagic-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file simplemagic-0.1.0.tar.gz.

File metadata

  • Download URL: simplemagic-0.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for simplemagic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 21b6d5d09f2ee57280357e1068d2d0de93695c8c32112a923afd593d4622df5c
MD5 7f25c11f16cc9d62fc6a14992b7baac5
BLAKE2b-256 f8edf555e6d13cf501054fbe91bd9c5b921d1ab0bf3c07d2f23a070996216eb7

See more details on using hashes here.

File details

Details for the file simplemagic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: simplemagic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for simplemagic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ee38e58eaa3c9875172b40f23847bc948b9f517f29dcb95e9f740a3ce5793ed
MD5 e9747a75f04e7ab35b623b198d371bf5
BLAKE2b-256 fe4d2161a7ae1b100a7a8fb4015ae20fd5ef5f313663dca2d7cf00740579a768

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page