The Python mimetypes module is not particularly accurate and bases its results on a file extension. The only way to get a more accurate result is to invoke the Unix command fileusing the subprocess.Popenfollowing:
import subprocess
mimetype = subprocess.Popen(['file', '/path/to/file', '--mime-type', '-b'],
stdout=subprocess.PIPE).stdout.read().strip()
It feels inelegant. Is there a better way to do this without naming file, but still achieving the same level of accuracy?
Matty source
share