1

I'm looking for a python library that provides that returns exactly the same mime type for a given file as the bash command xdg-mime query filetype <filename>. The goal is to use that function to find all file in a diretory with a certain mime type (application/mbox)

I'm aware of the xdg library, but none of its functions returns the same value as the xdg-mime command.

konstin
  • 476
  • 7
  • 16

1 Answers1

0

You can supply your own mapping to the python mimetypes module - so You can call xdg-mime on all the extensions you need and then create a file that you feed to mimetypes like this:

import mimetypes
mimetypes.init()
extra={".123": "application/vnd.lotus-1-2-3"}
[mimetypes.add_type(value, key) for key, value in extra.items()]

# testing

mimetypes.guess_type("x.123")

gives "application/vnd.lotus-1-2-3"

user2692263
  • 475
  • 4
  • 8
  • `xdg-mime` doesn't rely on the file extensions, so this won't work. In my case the files don't even have an extension. – konstin Nov 22 '15 at 18:01
  • There is a python binding to libmagic as well - I guess that is the most straight forward option when bypassing file extension. https://github.com/ahupp/python-magic – user2692263 Nov 22 '15 at 18:29
  • libmagic only says `ASCII text, with CRLF, LF line terminators` – konstin Nov 23 '15 at 19:23