Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

content-type text/plain has file extension .ksh?

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

like image 560
guettli Avatar asked Dec 24 '22 02:12

guettli


1 Answers

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.

like image 182
Chris_Rands Avatar answered Dec 25 '22 15:12

Chris_Rands