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

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

    I'm surprised that nobody has mentioned it but Pygments is able to make an educated guess about the mime-type of, particularly, text documents.

    Pygments is actually a Python syntax highlighting library but is has a method that will make an educated guess about which of 500 supported document types your document is. i.e. c++ vs C# vs Python vs etc

    import inspect
    
    def _test(text: str):
        from pygments.lexers import guess_lexer
        lexer = guess_lexer(text)
        mimetype = lexer.mimetypes[0] if lexer.mimetypes else None
        print(mimetype)
    
    if __name__ == "__main__":
        # Set the text to the actual defintion of _test(...) above
        text = inspect.getsource(_test)
        print('Text:')
        print(text)
        print()
        print('Result:')
        _test(text)
    

    Output:

    Text:
    def _test(text: str):
        from pygments.lexers import guess_lexer
        lexer = guess_lexer(text)
        mimetype = lexer.mimetypes[0] if lexer.mimetypes else None
        print(mimetype)
    
    
    Result:
    text/x-python
    

    Now, it's not perfect, but if you need to be able to tell which of 500 document formats are being used, this is pretty darn useful.

提交回复
热议问题