简体   繁体   English

如何使用具有 MIME 类型“text/plain”的 Python 编写一个字符文件?

[英]How to write a one character file using Python with mime type `text/plain`?

How can I write a one character file in Python 3.8 that has the text/plain mime type?如何在 Python 3.8 中编写一个具有text/plain mime 类型的单字符文件?

Using Python, if I write one character to a file, the mime type of the file is application/octet-stream .使用 Python,如果我将一个字符写入文件,则文件的 mime 类型为application/octet-stream

with open('file_from_python', 'w') as f: f.write('x')

(Note: Writing more than one character using the Python code above results in the text/plain mime type; the issue only occurs when you write less than two characters to a file). (注意:使用上面的 Python 代码写入多个字符会导致text/plain mime 类型;仅当您向文件写入少于两个字符时才会出现此问题)。

Check the mime type of the python generated file using the following Linux command line:使用以下 Linux 命令行检查 python 生成文件的 mime 类型:

file --mime-type file_from_python 
file_from_python: application/octet-stream

Using the Linux terminal, if I write one character to a file, the mime type of the file is text/plain .使用 Linux 终端,如果我将一个字符写入文件,则文件的 mime 类型为text/plain

echo "x" > file_from_bash

Check the mime type of the Linux terminal generated file using the following Linux command line:使用以下 Linux 命令行检查 Linux 终端生成文件的 mime 类型:

file --mime-type file_from_bash
file_from_bash: text/plain

The file command is just applying heuristics to give a pretty good guess at the file type. file命令只是应用启发式方法来很好地猜测文件类型。 It can't guess very well for a 1 byte file, so it gives up.它不能很好地猜测一个 1 字节的文件,所以它放弃了。

This bit of Python will produce a file identical to your bash command: Python 的这一位将生成与您的 bash 命令相同的文件:

with open('file_from_python_text', 'w') as f: f.write('x\n')

Echo adds a newline. Echo 添加一个换行符。

Not sure how your app checks mime type but you should be aware that mime type is not really associated with file content.不确定您的应用如何检查 mime 类型,但您应该知道 mime 类型与文件内容并没有真正关联。 It's derrived from it (because that's the only sane way) but there's nothing preventing you say that ie JPG file is text/plain and plain text file is application/octet-stream or even image/jpeg .它是由它派生的(因为这是唯一理智的方式),但没有什么能阻止你说 JPG 文件是text/plain而纯文本文件是application/octet-stream甚至是image/jpeg In other words you can easily tell what mime type it is based on your own rules if needed.换句话说,如果需要,您可以根据自己的规则轻松判断它是什么 mime 类型。 Also file opinion is not definitive nor authoritative even if it's often right. file意见也不是确定的,也不是权威的,即使它通常是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python SimpleHttpServer,如何以纯文本/ MIME类型返回不带扩展名的文件? - Python SimpleHttpServer, how to return files without extensions with plain/text mime type? 如何查找python中文件的mime类型? - How to find the mime type of a file in python? python 2 Web服务器本地主机错误。 服务器以非JavaScript MIME类型“ text / plain”进行响应 - python 2 web server local host error. The server responded with a non-JavaScript MIME type of “text/plain” 使用Python以纯文本格式读取二进制文件 - Reading a binary file as plain text using Python jinja2静态文件mime-type始终是text / plain - jinja2 static files mime-type always text/plain 使用python从文本文件中删除除一个换行符以外的所有字符 - Removing all but one newline character from text file using python 如何以纯文本模式发送电子邮件换行符Python 3作为UTF8的MIME替代? - How do I send email line feeds Python 3 in plain text mode as a MIME alternate with UTF8? 如何从纯文本文件中读取随机字符 - How do I read a random character from a plain text file 如何在不使用Magic库的情况下在python中查找内容文件的mime类型? - How to find the mime type of a content file in python without using Magic library? 如何使用Python请求发送文本/纯内容类型的POST(请求有效负载)? - How to sent a POST(request payload) with text/plain content type using Python-Requests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM