除了 EOF 之外,Python 的 read() 是否总是返回请求的大小?

发布于 2024-12-03 23:40:13 字数 105 浏览 0 评论 0原文

Python read() 方法的行为与 C 的 read 类似吗?在到达文件的最后一个块之前,它返回的字节数是否可能少于请求的字节数?或者当这些字节存在可供读取时,它是否保证始终返回全部字节数?

Does the Python read() method behave like C's read? Might it return less than the requested number of bytes before the last chunk of the file is reached? Or does it guarantee to always return the full amount of bytes when those bytes exist to be read?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放血 2024-12-10 23:40:13

好吧,Python 标准库对 file.read([size]) 是这么说的:

从文件中最多读取 size 字节(如果在获取 size 字节之前读取达到 EOF,则读取的字节数会更少)。如果 size 参数为负数或省略,读取所有数据直到达到 EOF。 ... 立即遇到 EOF 时返回空字符串。 ...另请注意,在非阻塞模式下,即使没有给出大小参数,返回的数据也可能少于请求的数据。

Well, the Python Standard library says this about file.read([size]):

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. ... An empty string is returned when EOF is encountered immediately. ... Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.

回忆凄美了谁 2024-12-10 23:40:13

在 CPython 上,它将始终返回请求的字节数,除非达到 EOF。

On CPython, it will always return the number of bytes requested, unless EOF is reached.

狠疯拽 2024-12-10 23:40:13

这实际上取决于正在阅读的内容。

默认情况下,python 使用 io.BufferedReader 但不会使用如果您显式关闭缓冲,则为一:


with open(__file__, "rb", buffering=0) as file:
    print(f"Unbuffered file type {type(file).__name__}")

with open(__file__, "rb", buffering=10) as file:
    print(f"Explicit buffered file type {type(file).__name__}")

with open(__file__, "rb") as file:
    print(f"Default [buffered] file type {type(file).__name__}")
Unbuffered file type FileIO
Explicit buffered file type BufferedReader
Default [buffered] file type BufferedReader

根据文档,io.BufferedReader 只有在遇到 EOF 或底层操作系统调用会阻塞时才会读取较少的内容:

读取(大小=- 1, /)

读取并返回 size 字节,或者如果未给出 size
或负数,直到 EOF 或如果读取调用将
以非阻塞模式阻塞。

这意味着如果您正在读取常规文件,则 BufferedReader 只有在遇到 EOF 时才会读取较少的内容,因为常规文件不会“阻塞”,即使它们需要时间返回。

但是,如果您正在从 *nix“FIFO”(管道)读取数据,那么如果管道中没有数据等待,io.BufferedReader 将返回少于请求的数据。

如果完全禁用缓冲,那么 read() 将返回操作系统返回的任何内容。即使您正在读取常规文件,返回的内容也可能少于您请求的内容。禁用缓冲时要非常小心。

This really depends on what is being read.

By default python uses a io.BufferedReader but will not use one if you explicitly switch off buffering:


with open(__file__, "rb", buffering=0) as file:
    print(f"Unbuffered file type {type(file).__name__}")

with open(__file__, "rb", buffering=10) as file:
    print(f"Explicit buffered file type {type(file).__name__}")

with open(__file__, "rb") as file:
    print(f"Default [buffered] file type {type(file).__name__}")
Unbuffered file type FileIO
Explicit buffered file type BufferedReader
Default [buffered] file type BufferedReader

According to the documentation, the io.BufferedReader will only read less if it hits the EOF or the underlying OS call would block:

read(size=- 1, /)

Read and return size bytes, or if size is not given
or negative, until EOF or if the read call would
block in non-blocking mode.

That means if you are reading a regular file, the BufferedReader will only read less if it hits the EOF because regular files do not "block", even if they take time to return.

However if you are reading from a *nix "FIFO" (pipe) then the io.BufferedReader will return less than requested if there is no data waiting in the pipe.

If you disable buffering altogether then read() will return whatever the OS returns. Even if you are reading regular files, this can return less than you requested. Be very careful when disabling buffering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文