June 2017
Beginner
352 pages
8h 39m
English
To read the file back we use open() again, but this time we pass 'rt', for read-text, as the mode:
>>> g = open('wasteland.txt', mode='rt', encoding='utf-8')
If we know how many bytes to read, or if we want to read the whole file, we can use read(). Looking back through our REPL we can see that the first write was 32 characters long, so let's read that back with a call to the read() method:
>>> g.read(32)'What are the roots that clutch, '
In text mode, the read() method accepts the number of characters to read from the file, not the number of bytes. The call returns the text and advances the file pointer to the end of what was read. Because we opened the file in text mode, the return type is str.
To read all the remaining data ...
Read now
Unlock full access