Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

But what value does 'line' have when the loop exits? The Python 3 docs only read:

readline(size=-1)

Read and return one 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 newline argument to open() can be used to select the line terminator(s) recognized.

Edit:

In my version of Python (3.6.1), if you open a file in binary mode, help(file_in.readline) gives

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)

like image 796
Josiah Yoder Avatar asked Jul 10 '17 18:07

Josiah Yoder


People also ask

What does readline return at end of file Python?

In addition to the for loop, Python provides three methods to read data from the input file. The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end.

What does readline () method return in Python?

The readlines() method returns a list containing each line in the file as a list item.

What's the return value readline () method of file object in Python?

The readline() method returns one line from the file.

How does Python handle end of file?

Python doesn't have built-in eof detection function but that functionality is available in two ways: f. read(1) will return b'' if there are no more bytes to read. This works for text as well as binary files. The second way is to use f.

What is the use of readline in Python?

What is Python readline? Python readline () is a file method that helps to read one complete line from the given file. It has a trailing newline ("\n") at the end of the string returned. You can also make use of the size parameter to get a specific length of the line.

How to read only one complete line from a Python file?

Python readline () method reads only one complete line from the file given. It appends a newline ("n") at the end of the line. If you open the file in normal read mode, readline () will return you the string. If you open the file in binary mode, readline () will return you binary object.

What is the difference between readline() and stripping() in Python?

And yes, readline simply returns the next line of text available from the given file object. if the file is empty readline () returns a blank string. if second line is the last line in the file and doesn't end with newline, then stripping () fixes the equality test. Not stripping fails the substring equality.

How to read one line at a time in Python?

Summary 1 Python readline () is a file method that helps to read one complete line from the given file. ... 2 You can also make use of the size parameter to get a specific length of the line. ... 3 The readline () method helps to read just one line at a time, and it returns the first line from the file given. ... More items...


1 Answers

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

f.readline() reads a single line from the file; 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. 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.

like image 117
yinnonsanders Avatar answered Nov 15 '22 08:11

yinnonsanders