Reading multiple lines at once

Let's rewind our file pointer again and read our file in a different way:

>>> g.seek(0)

Sometimes when we know we want to read every line in the file — and if we're sure we have enough memory to do so — we can read all lines from the file into a list with the readlines() method:

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

This is particularly useful if parsing the file involves hopping backwards and forwards between lines; it's much easier to do this with a list of lines than with a file stream of characters.

This time, we'll close the file before moving on:

>>> g.close()
..................Content has been hidden....................

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