June 2017
Beginner
352 pages
8h 39m
English
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\n','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()
Read now
Unlock full access