How to find the mime type of a file in python?

前端 未结 19 966
猫巷女王i
猫巷女王i 2020-11-22 15:19

Let\'s say you want to save a bunch of files somewhere, for instance in BLOBs. Let\'s say you want to dish these files out via a web page and have the client automatically o

相关标签:
19条回答
  • 2020-11-22 15:58

    More reliable way than to use the mimetypes library would be to use the python-magic package.

    import magic
    m = magic.open(magic.MAGIC_MIME)
    m.load()
    m.file("/tmp/document.pdf")
    

    This would be equivalent to using file(1).

    On Django one could also make sure that the MIME type matches that of UploadedFile.content_type.

    0 讨论(0)
提交回复
热议问题