从文件内容中检测文件类型,而不是文件名

时间:2012-08-11 18:49:01

标签: python

  

可能重复:
  How to find the mime type of a file in python?

我想检测文件的类型,但我不想查看扩展名,因为它可能不正确。 mimetypes模块基于文件名。

是否有标准的方式来查看文件?

1 个答案:

答案 0 :(得分:7)

尝试安装python-magic模块。

 >>> mime = magic.Magic(mime=True)
 >>> mime.from_file("testdata/abc.pdf")
'application/pdf'