Skip to main content

A Python API for libmagic, the library behind the Unix file command

Project description

Background

libmagic is the library that commonly supports the file command on Unix system, other than Max OSX which has its own implementation. The library handles the loading of database files that describe the magic numbers used to identify various file types, as well as the associated mime types. The library also handles character set detections.

Installation

Before installing filemagic, the libmagic library will need to be availabile. To test this is the check for the presence of the file command and/or the libmagic man page.

$ which file
$ man libmagic

On Mac OSX, Apple has implemented their own version of the file command. However, libmagic can be installed using homebrew

$ brew install libmagic

After brew finished installing, the test for the libmagic man page should pass.

Now that the presence of libmagic has been confirmed, use pip to install filemagic.

$ pip install filemagic

The magic module should now be availabe from the Python shell.

>>> import magic

The next section will describe how to use the magic.Magic class to identify file types.

Usage

The magic module uses ctypes to wrap the primitives from libmagic in the more user friendly magic.Magic class. This class handles initialization, loading databases and the release of resources.

>>> import magic

To ensure that resources are correctly released by magic.Magic, it’s necessary to either explicitly call magic.Magic.close on instances, or use with statement.

>>> with magic.Magic as m:
...     pass
...

magic.Magic supports context managers which ensures resources are correctly released at the end of the with statements irrespective of any exceptions.

To identify a file from it’s filename, use the magic.Magic.id_filename() method.

>>> with magic.Magic as m:
...     m.id_filename('setup.py')
...
'Python script, ASCII text executable'

Similarily to identify a file from a string that has already been read, use the magic.Magic.id_buffer method.

>>> with magic.Magic as m:
...     m.id_buffer('#!/usr/bin/python\n')
...
'Python script, ASCII text executable'

To identify with mime type, rather than a textual description, pass the magic.MAGIC_MIME_TYPE flag when creating the magic.Magic instance.

>>> with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
...     m.id_filename('setup.py')
...
'text/x-python'

Similarily, magic.MAGIC_MIME_ENCODING can be passed to return the encoding type.

>>> with magic.Magic(flags=magic.MAGIC_MIME_ENCODING) as m:
...     m.id_filename('setup.py')
...
'us-ascii'

Unicode and filemagic

On both Python2 and Python3, magic.Magic’s methods will encode any unicode objects (the default string type for Python3) to byte strings before being passed to libmagic. On Python3, returned strings will be decoded to unicode using the default encoding type. The user should not be concerned whether unicode or bytes are passed to magic.Magic methods. However, the use will need to be aware that return strings are always unicode on Python3 and byte strings on Python2.

Reporting issues

The source code for filemagic is hosted on Github. Problems can be reported using Github’s issues tracking system.

filemagic has been tested against libmagic 5.11. Continuous integration is provided by Travis CI. The current build status is build_status.

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

filemagic-1.3.tar.gz (19.7 kB view details)

Uploaded Source

File details

Details for the file filemagic-1.3.tar.gz.

File metadata

  • Download URL: filemagic-1.3.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for filemagic-1.3.tar.gz
Algorithm Hash digest
SHA256 0541ac134c9eebfc0144c7802de244797f6f42e414bd73b747116e61e94f5839
MD5 9d01cc627b26191d8b8c99b59dc33efb
BLAKE2b-256 bb43cb3148ff55966128feb400b622d21b8b5ce3caed75ec023b4aff55c252c4

See more details on using hashes here.

Supported by

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