如何在 Python 3.8 中编写具有 text/plain
mime 类型的单字 rune 件?
使用 Python,如果我向文件写入一个字符,则该文件的 mime 类型为 application/octet-stream
。
with open('file_from_python', 'w') as f: f.write('x')
(注意:使用上述 Python 代码写入多个字符会导致 text/plain
mime 类型;仅当您向文件写入少于两个字符时才会出现此问题)。
使用以下 Linux 命令行检查 python 生成的文件的 mime 类型:
file --mime-type file_from_python
file_from_python: application/octet-stream
使用Linux终端,如果我向文件写入一个字符,则该文件的mime类型为text/plain
。
echo "x" > file_from_bash
使用以下 Linux 命令行检查 Linux 终端生成的文件的 mime 类型:
file --mime-type file_from_bash
file_from_bash: text/plain
最佳答案
file命令只是应用启发式方法来很好地猜测文件类型。它无法很好地猜测 1 字节文件,因此放弃。
这部分 Python 将生成一个与您的 bash 命令相同的文件:
with open('file_from_python_text', 'w') as f: f.write('x\n')
Echo 添加换行符。
关于python - 如何使用Python编写一个mime类型为 `text/plain`的单字 rune 件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62617361/