Puremagic is a pure Python module designed to identify file types based on their 'magic numbers' and content-aware analysis. It offers a lightweight, cross-platform alternative to `python-magic`/`libmagic` with zero runtime dependencies. The library is actively maintained, with its current version being 2.2.0, and receives regular updates to enhance detection capabilities and fix issues.
pip install puremagicVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `puremagic.from_file()` to get the most likely file extension and `puremagic.magic_file()` to retrieve all detected magic numbers, MIME types, and confidence levels. It also includes an example for in-memory string detection using `puremagic.magic_string()`.
Upgrade your Python environment to 3.12+ or use the 1.x release chain for older Python versions.
Replace calls to `puremagic.what()` with `puremagic.from_file()` for similar functionality.
Update any code that relies on the exact MIME type string for WAV files to use `audio/wav`.
Review existing code and expected outputs, especially for files that were ambiguously identified before. If necessary, deep scan can be disabled by setting the environment variable `PUREMAGIC_DEEPSCAN=0`.
Install the library using pip: `pip install puremagic`.
Handle the `puremagic.PureError` exception in your code to gracefully manage cases where a file type cannot be identified. For example: `try: ext = puremagic.from_file(filename) except puremagic.PureError: print('File type could not be determined.')`Ensure that the file path provided to `puremagic` is correct and that the file exists at that location. Use an absolute path or verify the relative path from your current working directory. For example, `puremagic my_existing_file.txt` or `puremagic /path/to/my_file.jpg`.
Review the input passed to the `puremagic` function that raised the error. Ensure that file contents are not empty, paths are valid strings, and any optional arguments adhere to the expected types and formats.
No dependency data recorded yet.