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...")

Example files detected

ok.conf: text/plain
ok.coverage: application/vnd.sqlite3
ok.dat: application/octet-stream
ok.doc: application/msword
ok.docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
ok.dot: application/vnd.openxmlformats-officedocument.wordprocessingml.document
ok.in: text/plain
ok.ini: text/plain
ok.java: text/x-java
ok.jpg: image/jpeg
ok.log: text/plain
ok.md: text/plain
ok.pages: application/zip
ok.pdf: application/pdf
ok.pl: text/x-perl
ok.png: image/png
ok.pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
ok.py: text/x-script.python
ok.sh: text/x-shellscript
ok.sql: text/plain
ok.tar.gz: application/gzip
ok.txt: text/plain
ok.txt.bz2: application/x-bzip2
ok.whl: application/zip
ok.wps: application/vnd.openxmlformats-officedocument.wordprocessingml.document
ok.wpt: application/vnd.openxmlformats-officedocument.wordprocessingml.document
ok.wsdl: text/xml
ok.xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
ok.xmind: application/zip
ok.xml: text/xml
ok.xsl: text/xml
ok.yml: text/plain
ok.zip: application/zip

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.

v0.1.1

  • Recover stream position after mimetype detect.
  • Fix small file handling problem in puremagic.
  • Fix .gz extension problem.
  • Fix .bz2 extension problem.

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.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

simplemagic-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simplemagic-0.1.1.tar.gz
  • Upload date:
  • Size: 8.2 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.1.tar.gz
Algorithm Hash digest
SHA256 870dbbb45d6dbb4f5a52f42038d2938f07ffc8ddff48235ce9475f4e90138564
MD5 dc8d46be31ea75242d2d55fff499cffb
BLAKE2b-256 fca2bc016e7eaf736af77f3eb93e6d541df36a0614a39f466028a2915a6b0ae6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplemagic-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 46d6fcd4561331c7c02ed28676720d20430617735b497c3d478705220785a838
MD5 d4fd830c38f9b3423c6d51bbc5b67698
BLAKE2b-256 604c6604809ee0e791c97e5b29bdf47cd3d97c4520e912d3134f99e64f3ffe63

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