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

前端 未结 19 998
猫巷女王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:32

    The python-magic method suggested by toivotuo is outdated. Python-magic's current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.

    # For MIME types
    import magic
    mime = magic.Magic(mime=True)
    mime.from_file("testdata/test.pdf") # 'application/pdf'
    

提交回复
热议问题