python - file.read() 是否保证读取整个文件?

标签 python text

file.read() 的文档说:

    Notice that when in non-blocking mode, less data than what was requested
    may be returned, even if no size parameter was given.

这是否意味着 file.read() 不能保证始终返回文本文件的全部内容,即使它很可能会返回? 如果是这样,读取整个文本文件的正确方法是什么?

最佳答案

对于非常规文件的"file",如果您专门打开 file.read() 不尝试读取所有内容的模式,那么file.read() 可能无法读取所有内容。

文档指定了非阻塞模式。非阻塞模式是可以在file descriptor上设置的模式。在 Unix 中,这只对代表套接字、管道或其他通信设备的"file"有效。在非阻塞模式下,如果您尝试读取比当前可用的数据更多的数据,文件将为您提供它所拥有的数据,而不是等到足够的数据到达。

关于python - file.read() 是否保证读取整个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52901029/

相关文章:

python - 如何在 Python 中正确解开此 json 响应以将我需要的数据放入 Pandas DataFrame 中?

python - 类型错误 : Variable to save is not a Variable

css - 动态删除来自行高的文本上下不必要的空间

python - 如何使用 python 和 regex 合并两个文本文件

python - 改变 networkx/matplotlib 力图中的边长和簇间距

javascript - DatetimeProperty 到 Javascript 时间

python - 需要 Pygame 帮助

python - 如何使用Pandas通过特殊标记逐节读取Txt文件?

batch-file - xcopy 循环复制 .txt 文件

python - 使用 Vagrant 托管 PostgreSQL 数据库时如何找到我的同步文件夹?