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

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

    2017 Update

    No need to go to github, it is on PyPi under a different name:

    pip3 install --user python-magic
    # or:
    sudo apt install python3-magic  # Ubuntu distro package
    

    The code can be simplified as well:

    >>> import magic
    
    >>> magic.from_file('/tmp/img_3304.jpg', mime=True)
    'image/jpeg'
    

提交回复
热议问题