Filemagic

A Python library providing bindings for libmagic to easily identify file types from their magic numbers.

What it is

Filemagic is a Python API wrapper for the powerful libmagic library, the technology behind the Unix file command. It allows developers to programmatically determine the type of a file by examining its magic number bytes, a fundamental method for robust file identification. This tool is essential for applications that need to process uploads, manage files, or automate workflows based on file content rather than just extensions.

The library offers a simple and clean Python interface to check both files stored on disk and data held in memory as strings. It can return a human-readable textual description of the file, its MIME type, or its MIME encoding. For advanced use cases, it supports the use of custom magic files, enabling highly specialized and tailored file detection rules.

Filemagic is designed to help Python developers, system administrators, and software engineers build more reliable and secure applications. It is particularly useful for those working on web platforms that handle user uploads, data processing pipelines, digital asset management systems, or any software requiring accurate file type validation.

It is a cross-platform solution with support for both Python 2 and 3, as well as compatibility with CPython and PyPy interpreters. The project provides comprehensive documentation hosted on Read the Docs, including a user guide, API reference, and command-line invocation details. Its notable feature is bringing a proven, Unix-standard identification capability directly into the Python ecosystem.

This description was generated by AI from the site's public content and may contain errors.

What you can do there

  • Read library documentation and user guide
  • Learn how to integrate file type detection into Python code
  • Use the command line tool for file identification
  • Access the API reference for developers
  • Find information on reporting bugs and issues

Who it's for

Python developers, system administrators, and software engineers building applications that require file type identification.

Frequently asked questions about filemagic.readthedocs.io

›What is filemagic used for?

Filemagic is used to programmatically identify the type of a file by its contents, which is crucial for validating uploads, processing data, and ensuring application security.

›How does filemagic work?

It works as a Python interface to the libmagic library, which checks the magic number bytes at the beginning of a file against a extensive database of signatures.

›Can filemagic check files in memory?

Yes, it can identify files from both disk paths and data stored in memory as strings, offering flexibility for different application architectures.

›Does it support custom file type rules?

Yes, you can provide custom magic files to extend or customize the library's detection capabilities for specialized formats.

›Is filemagic compatible with PyPy?

Yes, it supports both the standard CPython interpreter and PyPy, as well as both Python 2 and Python 3.

›Where can I find the documentation?

Full documentation is hosted on Read the Docs, containing a guide, API reference, and instructions for command line use.

›How do I report an issue with the library?

The documentation includes a section on reporting issues to guide users on how to submit bugs or problems to the project maintainers.