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

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

    Python bindings to libmagic

    All the different answers on this topic are very confusing, so I’m hoping to give a bit more clarity with this overview of the different bindings of libmagic. Previously mammadori gave a short answer listing the available option.

    libmagic

    • module name: magic
    • pypi: file-magic
    • source: https://github.com/file/file/tree/master/python

    When determining a files mime-type, the tool of choice is simply called file and its back-end is called libmagic. (See the Project home page.) The project is developed in a private cvs-repository, but there is a read-only git mirror on github.

    Now this tool, which you will need if you want to use any of the libmagic bindings with python, already comes with its own python bindings called file-magic. There is not much dedicated documentation for them, but you can always have a look at the man page of the c-library: man libmagic. The basic usage is described in the readme file:

    import magic
    
    detected = magic.detect_from_filename('magic.py')
    print 'Detected MIME type: {}'.format(detected.mime_type)
    print 'Detected encoding: {}'.format(detected.encoding)
    print 'Detected file type name: {}'.format(detected.name)
    

    Apart from this, you can also use the library by creating a Magic object using magic.open(flags) as shown in the example file.

    Both toivotuo and ewr2san use these file-magic bindings included in the file tool. They mistakenly assume, they are using the python-magic package. This seems to indicate, that if both file and python-magic are installed, the python module magic refers to the former one.

    python-magic

    • module name: magic
    • pypi: python-magic
    • source: https://github.com/ahupp/python-magic

    This is the library that Simon Zimmermann talks about in his answer and which is also employed by Claude COULOMBE as well as Gringo Suave.

    filemagic

    • module name: magic
    • pypi: filemagic
    • source: https://github.com/aliles/filemagic

    Note: This project was last updated in 2013!

    Due to being based on the same c-api, this library has some similarity with file-magic included in libmagic. It is only mentioned by mammadori and no other answer employs it.

    0 讨论(0)
  • 2020-11-22 15:48

    You didn't state what web server you were using, but Apache has a nice little module called Mime Magic which it uses to determine the type of a file when told to do so. It reads some of the file's content and tries to figure out what type it is based on the characters found. And as Dave Webb Mentioned the MimeTypes Module under python will work, provided an extension is handy.

    Alternatively, if you are sitting on a UNIX box you can use sys.popen('file -i ' + fileName, mode='r') to grab the MIME type. Windows should have an equivalent command, but I'm unsure as to what it is.

    0 讨论(0)
  • 2020-11-22 15:50

    This may be old already, but why not use UploadedFile.content_type directly from Django? Is not the same?(https://docs.djangoproject.com/en/1.11/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.content_type)

    0 讨论(0)
  • 2020-11-22 15:51

    For byte Array type data you can use magic.from_buffer(_byte_array,mime=True)

    0 讨论(0)
  • 2020-11-22 15:53

    python 3 ref: https://docs.python.org/3.2/library/mimetypes.html

    mimetypes.guess_type(url, strict=True) Guess the type of a file based on its filename or URL, given by url. The return value is a tuple (type, encoding) where type is None if the type can’t be guessed (missing or unknown suffix) or a string of the form 'type/subtype', usable for a MIME content-type header.

    encoding is None for no encoding or the name of the program used to encode (e.g. compress or gzip). The encoding is suitable for use as a Content-Encoding header, not as a Content-Transfer-Encoding header. The mappings are table driven. Encoding suffixes are case sensitive; type suffixes are first tried case sensitively, then case insensitively.

    The optional strict argument is a flag specifying whether the list of known MIME types is limited to only the official types registered with IANA. When strict is True (the default), only the IANA types are supported; when strict is False, some additional non-standard but commonly used MIME types are also recognized.

    import mimetypes
    print(mimetypes.guess_type("sample.html"))
    
    0 讨论(0)
  • 2020-11-22 15:55

    The mimetypes module just recognise an file type based on file extension. If you will try to recover a file type of a file without extension, the mimetypes will not works.

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