Readline line by line

Using read() for text is quite awkward, and thankfully Python provides better tools for reading text files line by line. The first of these is readline() function:

>>> g.readline()
'What are the roots that clutch, what branches grow '
>>> g.readline()
'Out of this stony rubbish? '

Each call to readline() returns a single line of text. The returned lines are terminated by a single newline character, if there is one present in the file. The last line here does not terminate with a newline because there is no newline sequence at the end of the file. You shouldn't rely on the string returned by readline() being terminated by a newline. And remember that the universal newline support will have translated whatever the platform native newline sequence into ' '.

Once we reach the end of the file further calls to readline() return an empty string:

>>> g.readline()
''
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset