python - 内容类型 text/plain 的文件扩展名为 .ksh?

标签 python mime-types

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'

我怎样才能得到有效的答案?

对我来说,“.txt”很合适。

即使是 filetype lib 无法处理这个:-(

参见 https://github.com/h2non/filetype.py/issues/30

最佳答案

要获得与 Python 3 和 2 一致的输出,您需要使用 guess_all_extensions 并对输出进行排序:

>>> 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 是最后一项。

奇怪的是这些还没有排序,因为 guess_extension 只需要 the first arbitrary extension ,因此您会观察到不同的输出。

关于python - 内容类型 text/plain 的文件扩展名为 .ksh?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53541343/

相关文章:

css - 网络服务器 + Chrome v40 + font awesome 不起作用

python - Matplotlib 直方图标题

c# - 我如何知道字符串是否代表有效的 MIME 类型?

php - 确定 excel 文件 mime 类型

ios - 设置了正确的内容类型,但 .ipa 文件被视为 .zip 文件

javascript - X-内容类型-选项 :nosniff in DWR

python - 函数内部变量的赋值会改变外部的赋值 - Python

python - 在Python中通过jq从API过滤JSON

python - 如何通过 boto 获得当前现货价格?

python - 在 Python 程序中使用 SQLite