Home > Software engineering >  Python mimetypes on Windows missing some extensions
Python mimetypes on Windows missing some extensions

Time:10-12

I want mimetypes library to return mimetype from file name. For example:

>>> mimetypes.types_map['.docx']
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'

But, currently I got an exception:

>>> mimetypes.types_map['.docx']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
KeyError: '.docx'

Also I tried guess_type func:

>>> mimetypes.guess_type('.docx')
(None, None)

Full code:

import mimetypes
mimetypes.init()
mimetypes.types_map['.docx']  # throws an Exception
mimetypes.guess_type('.docx')  # returns (None, None)

Python: 3.8.10

Windows 10 build ver. 19044.2006

On my system I got installed Ubuntu WSL 2. When executing my Full code there: no exceptions are raised and mimetype returns as excepted (also python 3.8.10)

CodePudding user response:

You need to pass a "name" before dot.

>>> mimetypes.guess_type('.docx')
(None, None)
>>> mimetypes.guess_type('filename.docx')
('application/vnd.openxmlformats-officedocument.wordprocessingml.document', None)

Docs: https://docs.python.org/3/library/mimetypes.html

CodePudding user response:

Repair of Office package via Control Panel updated regedit and added missing ContentType records.

I chose offline mode during the repair.

  • Related