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
  • simplemagic.is_file_content_matches_with_file_extension # mostly we just use this function to check if the file cotent is matches with the file extension.

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.bash_history: text/plain
ok.bash_profile: text/plain
ok.bashrc: text/plain
ok.conf: text/plain
ok.coverage: application/vnd.sqlite3
ok.csv: text/plain
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.dps: application/vnd.openxmlformats-officedocument.presentationml.presentation
ok.dpt: application/vnd.openxmlformats-officedocument.presentationml.presentation
ok.et: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
ok.ett: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
ok.gif: image/gif
ok.gitignore: text/plain
ok.htaccess: text/plain
ok.in: text/plain
ok.ini: text/plain
ok.java: text/x-java
ok.jpg: image/jpeg
ok.less: text/plain
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.properties: text/plain
ok.py: text/x-script.python
ok.rpm: application/x-rpm
ok.scss: text/plain
ok.sh: text/x-shellscript
ok.sql: text/plain
ok.svg: image/svg+xml
ok.tar.gz: application/gzip
ok.ttf: font/sfnt
ok.txt: text/plain
ok.txt.bz2: application/x-bzip2
ok.whl: application/zip
ok.woff: application/octet-stream
ok.woff2: application/octet-stream
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
private.DS_Store: application/octet-stream
private.bpmn: text/xml
private.cab: application/vnd.ms-cab-compressed
private.class: application/x-java-applet
private.dll: application/x-dosexec
private.dmg: application/x-bzip2
private.doc: application/msword
private.dwg: image/vnd.dwg
private.fla: application/CDFV2
private.ftl: text/html
private.ico: image/vnd.microsoft.icon
private.img: application/octet-stream
private.inf: text/plain
private.jsp: text/html
private.mht: message/rfc822
private.mp4: video/mp4
private.mpp: application/vnd.ms-office
private.msi: application/x-msi
private.pcap: application/vnd.tcpdump.pcap
private.pps: application/vnd.ms-powerpoint
private.ppt: application/vnd.ms-powerpoint
private.psd: image/vnd.adobe.photoshop
private.pyc: application/x-bytecode.python
private.rar: application/x-rar
private.reg: text/x-ms-regedit
private.swf: application/x-shockwave-flash
private.tar: application/x-tar
private.tif: image/tiff
private.vsd: application/vnd.ms-office
private.xls: application/vnd.ms-excel
private.xps: application/zip
private.xsd: text/xml

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.

v0.1.5

  • Put function is_file_content_matches_with_file_extension to public.
  • Using magic.detect_from_fobj instead of magic.detect_from_content to improve the recognition.
  • Change register_mimetype_extensions' parameters, and fix the problem.
  • Fix .dps, .dpt, .et, .ett extension problems.
  • Fix .dox problem.
  • Fix .mptt problem.
  • Fix .csv problem.
  • Fix .pcap problem.
  • Fix .rpm problem.
  • Fix .dmg problem.
  • Fix .reg problem.
  • Fix .dwg problem.
  • Fix .xps problem.
  • Fix .ttf problem.
  • Fix .woff and .woff2 problem.
  • Fix java .class problem.
  • Fix .jsp problem.
  • Fix .less and .scss problem.
  • Fix .pyc problem.
  • Fix .fla problem.
  • Fix .vsd problem.

v0.1.7

  • Add magic_content_length parameter in function is_file_content_matches_with_file_extension to control the stream content read length locally.
  • Fix export api name 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.7.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

simplemagic-0.1.7-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simplemagic-0.1.7.tar.gz
  • Upload date:
  • Size: 10.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.7.tar.gz
Algorithm Hash digest
SHA256 45f82f93822e6efb6700d1e3c926196c0bad94a27163e540ca890462338db3e8
MD5 e990bc7eefcba16dd47c8c7ddfac6def
BLAKE2b-256 4b4b5cb5156001b878f4805eac4d4fe66b5e8eba57f889d67f435a6eadf98e65

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simplemagic-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 be71f488c01764f360d5e39413164a334a137f9065027afdd7975aabfc7041b1
MD5 66e5c47c3655dd818fef240972b17c1f
BLAKE2b-256 3ffba3cb8b4696bffae287d917b15e503c74368be12dd1ba72ccf50198e811b6

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