简体   繁体   English

在Python中到达文件末尾时readline返回什么值?

[英]What value does readline return when reaching the end of the file in Python?

One can use the classic loop 可以使用经典循环

file_in = open('suppliers.txt', 'r')
line = file_in.readline()

while line:
    line = file_in.readline()

to read through a file line-by-line in Python. 在Python中逐行读取文件。

But what value does 'line' have when the loop exits? 但是当循环退出时,'line'有什么价值? The Python 3 docs only read: Python 3文档只读:

readline(size=-1) readline的(大小= -1)

Read and return one line from the stream. 从流中读取并返回一行。 If size is specified, at most size bytes will be read. 如果指定了size,则最多读取大小字节。

The line terminator is always b'\\n' for binary files; 对于二进制文件,行终止符总是b'\\ n'; for text files, the newline argument to open() can be used to select the line terminator(s) recognized. 对于文本文件,open()的换行参数可用于选择已识别的行终止符。

Edit : 编辑

In my version of Python (3.6.1), if you open a file in binary mode, help(file_in.readline) gives 在我的Python(3.6.1)版本中,如果以二进制模式打开文件, help(file_in.readline)会给出

readline(size=-1, /) method of _io.BufferedReader instance

    Read and return a line from the stream.

    If size is specified, at most size bytes will be read.

    The line terminator is always b'\n' for binary files; for text
    files, the newlines argument to open can be used to select the line
    terminator(s) recognized.

which is exactly the same as the docs quoted above . 这与上面引用文档完全相同。 But, as noted by Steve Barnes , if you open the file in text mode, you get a useful comment. 但是,正如史蒂夫巴恩斯所指出的,如果你以文本模式打开文件,你会得到一个有用的评论。 (Oops! Copy-paste error on my part) (哎呀!我的复制粘贴错误)

From the tutorial: https://docs.python.org/3.6/tutorial/inputoutput.html#methods-of-file-objects 从教程: https//docs.python.org/3.6/tutorial/inputoutput.html#methods-of-file-objects

f.readline() reads a single line from the file; f.readline()从文件中读取一行; a newline character ( \\n ) is left at the end of the string, and is only omitted on the last line of the file if the file doesn't end in a newline. 换行符( \\n )留在字符串的末尾,如果文件没有以换行符结尾,则只在文件的最后一行省略。 This makes the return value unambiguous; 这使得返回值明确无误; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\\n' , a string containing only a single newline. 如果f.readline()返回一个空字符串,则表示已到达文件的末尾,而空行由'\\n' ,该字符串仅包含一个换行符。

In a python console opening a file, f , and then calling help on its readline method tells you exactly: 在Python控制台打开一个文件,F,然后在其ReadLine方法调用的帮助会告诉你到底:

>>> f = open('temp.txt', 'w')
>>> help(f.readline)
Help on built-in function readline:

readline(size=-1, /) method of _io.TextIOWrapper instance
    Read until newline or EOF.

    Returns an empty string if EOF is hit immediately.

Each readline operates on the remainder of the file from the current point onward so will eventually hit an EOF. 每个读取线从当前点开始对文件的其余部分进行操作,因此最终将达到EOF。

Note that if you open the file in binary mode, with rb rather than r , then rather than a <class '_io.TextIOWrapper'> object you will get a <class '_io.BufferedReader'> object - then the help message is different: 请注意,如果以二进制模式打开文件,使用rb而不是r ,那么您将获得<class '_io.BufferedReader'> <class '_io.TextIOWrapper'>对象而不是<class '_io.TextIOWrapper'> <class '_io.BufferedReader'>对象 - 然后帮助消息是不同的:

Help on built-in function readline:

readline(size=-1, /) method of _io.BufferedReader instance
    Read and return a line from the stream.

    If size is specified, at most size bytes will be read.

    The line terminator is always b'\n' for binary files; for text
    files, the newlines argument to open can be used to select the line
    terminator(s) recognized.

And when this method reaches the EOF it will return an empty byte array, b'' rather than an empty string. 当此方法到达EOF时,它将返回一个空字节数组, b''而不是空字符串。

Note that all the above was tested with python 3.6 on Win10. 请注意,以上所有内容都是在Win10上使用python 3.6进行测试的。

Running the code snippet from the question in a (Python 3) console shows that it returns an empty string, or an empty Bytes object if opening the file in binary mode. 从(Python 3)控制台中的问题运行代码片段显示它返回空字符串,或者如果以二进制模式打开文件则返回空的Bytes对象。

Is this documented somewhere? 这是在某处记录的吗? Perhaps it is sort of a broad python standard? 也许这是一个广泛的python标准?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM