本文介绍了Python EOF用于file.read()的多字节请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上的Python文档指出立即遇到EOF时,将返回一个空字符串。文档进一步指出:

The Python docs on file.read() state that An empty string is returned when EOF is encountered immediately. The documentation further states:

我相信Guido提出了他不添加f.eof()的观点。,因此需要使用Python方式!

I believe Guido has made his view on not adding f.eof() PERFECTLY CLEAR so need to use the Python way!

尚不清楚但是,ME是一项确定的测试,如果您收到的读取次数少于请求的字节数,但您确实收到了一些数据,则表明您已达到EOF。

What is not clear to ME, however, is if it is a definitive test that you have reached EOF if you receive less than the requested bytes from a read, but you did receive some.

即:

with open(filename,'rb') as f:
    while True:
        s=f.read(size)
        l=len(s)
        if l==0:
            break     # it is clear that this is EOF...
        if l<size:
            break      # ? Is receiving less than the request EOF???

如果 break 是潜在错误吗?您收到的内容少于调用 file.read(size)所请求的字节数吗?

Is it a potential error to break if you have received less than the bytes requested in a call to file.read(size)?

推荐答案

您没有考虑蛇皮的存在……Python不是C。

You are not thinking with your snake skin on... Python is not C.

首先,进行审查:


  • st = f.read()读取为EOF,或者如果以二进制,到最后一个字节;

  • st = f.read(n)尝试读取 n 个字节,并且在任何情况下都不得超过 n 个字节;

  • st = f.readline()一次读取一行,该行以'\n'或EOF结尾;

  • st = f.readlines()使用readline()读取文件中的所有行并返回行列表。

  • st=f.read() reads to EOF, or if opened as a binary, to the last byte;
  • st=f.read(n) attempts to reads n bytes and in no case more than n bytes;
  • st=f.readline() reads a line at a time, the line ends with '\n' or EOF;
  • st=f.readlines() uses readline() to read all the lines in a file and returns a list of the lines.

如果文件读取方法位于EOF,则返回''。其他类型的 file like方法(例如StringIO,socket.makefile等)使用相同类型的EOF测试。从<$ c $返回小于 n 个字节的字节c> f.read(n)绝对不是对EOF的配置性测试!虽然该代码可能在99.99%的时间内都能正常工作,但对于无法正常工作的时间来说,这非常令人沮丧。加上,这是错误的Python形式。在这种情况下, n 的唯一用途是对返回值的大小设置上限。

If a file read method is at EOF, it returns ''. The same type of EOF test is used in the other 'file like" methods like StringIO, socket.makefile, etc. A return of less than n bytes from f.read(n) is most assuredly NOT a dispositive test for EOF! While that code may work 99.99% of the time, it is the times it does not work that would be very frustrating to find. Plus, it is bad Python form. The only use for n in this case is to put an upper limit on the size of the return.

类似Python文件的方法返回小于 个字节少于 n 个字节的原因有哪些?

What are some of the reasons the Python file-like methods returns less than n bytes?


  1. EOF肯定是一个常见原因;

  2. 网络套接字在读取时可能会超时,但保持打开状态;
  3. 恰好 n 个字节可能导致逻辑多字节字符之间的中断(例如 \r\n (在文本模式下,我认为是Unicode中的多字节字符)或某些您不知道的基础数据结构;

  4. 该文件位于非阻止模式,另一个进程开始访问该文件;

  5. 暂时无法访问该文件;

  6. 在发生错误时潜在的潜在错误条件文件,光盘,网络等。

  7. 程序接收到信号,但是信号处理程序忽略了它。

  1. EOF is certainly a common reason;
  2. A network socket may timeout on read yet remain open;
  3. Exactly n bytes may cause a break between logical multi-byte characters (such as \r\n in text mode and, I think, a multi-byte character in Unicode) or some underlying data structure not known to you;
  4. The file is in non-blocking mode and another process begins to access the file;
  5. Temporary non-access to the file;
  6. An underlying error condition, potentially temporary, on the file, disc, network, etc.
  7. The program received a signal, but the signal handler ignored it.

我将以这种方式重写您的代码:

I would rewrite your code in this manner:

with open(filename,'rb') as f:
    while True:
        s=f.read(max_size)
        if not s: break

        # process the data in s...

或者,编写:

def blocks(infile, bufsize=1024):
    while True:
        try:
            data=infile.read(bufsize)
            if data:
                yield data
            else:
                break
        except IOError as (errno, strerror):
            print "I/O error({0}): {1}".format(errno, strerror)
            break

f=open('somefile','rb')

for block in blocks(f,2**16):
    # process a block that COULD be up to 65,536 bytes long

这篇关于Python EOF用于file.read()的多字节请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:39