Python 2.7:
>>> from mimetypes import guess_extension
>>> guess_extension('text/plain')
'.ksh'
Python 3.5:
>>> from mimetypes import guess_extension
>>> guess_extension('text/plain')
'.c'
How can I get a valid answer?
For me ".txt" would fit.
Even the filetype lib can't handle this :-(
although the question mentioned mimetypes.guess_extension
, but it actually cannot be answered with the information in that module. mime type to extension mapping is one to multi, there is no weight info in the mimetypes
database, sorting extensions by alphabetical order could give a consistent answer, but apparently not what OP wants. I considered the following options:
by authority, IANA DB does not have extension information for every type, only a few types have this info and need hard work to parse.
by popularity, I hope there is one.
by consensus, an MDN wiki page named "Incomplete list of MIME types" is most close: it is actively maintained, it lists only one extension for some well-known mime type.
I guess the practical solution is, grab the table from the aforementioned MDN wiki, hard code those types, use mimetypes.guess_extension
as a fallback.
note you should take care of MDN content license.