Is there a python-equivalent of the unix "file" utility?

python, unix

Solution

- python-magic

- pymagic

Probably others as well. "magic" is the magic keyword to search for. ;-)

Problem

I want to have different behavior in a python script, depending on the type of file. I cannot use the filename extension as it may not be present or misleading. I could call the `file` utility and parse the output, but I would rather use a python builtin for portability. So is there anything in python that uses heuristics to deduce the type of the file from its contents?

Original source

Related problems