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

Question:

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?

Asked By: 0x89

||

Answers:

Probably others as well. “magic” is the magic keyword to search for. 😉

Answered By: Alex Brasetvik

I appreciate people who has responded and the marked answer seems to be correct and clean. But I’d like to highlight this answer from the similar question: https://stackoverflow.com/a/21499463/882187
I like this answer because the author has provided a code snippet that every visitor of SO could try.

Answered By: Vladimir Ignatyev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.