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 :-(
See https://github.com/h2non/filetype.py/issues/30
To get consistent outputs with Python 3 and 2, you need to use guess_all_extensions
and sort the output:
>>> from mimetypes import guess_all_extensions
>>> sorted(guess_all_extensions('text/plain'))
['.asc', '.bat', '.c', '.cc', '.conf', '.cxx', '.el', '.f90', '.h', '.hh', '.hxx', '.ksh', '.log', '.pl', '.pm', '.text', '.txt']
.txt
is the last item.
It's kinda odd these aren't already sorted since guess_extension
just takes the first arbitrary extension, hence the different outputs you observe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With