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

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

    This seems to be very easy

    >>> from mimetypes import MimeTypes
    >>> import urllib 
    >>> mime = MimeTypes()
    >>> url = urllib.pathname2url('Upload.xml')
    >>> mime_type = mime.guess_type(url)
    >>> print mime_type
    ('application/xml', None)
    

    Please refer Old Post

    Update - In python 3+ version, it's more convenient now:

    import mimetypes
    print(mimetypes.guess_type("sample.html"))
    

提交回复
热议问题