April 2017
Beginner to intermediate
312 pages
7h 23m
English
The seek function enables moving the pointer to a specific position and reading a byte or n bytes from that position. Let's re-visit reading write_file.txt and try to read the word excited in the sentence I am excited to learn Python using Raspberry Pi Zero.
if __name__ == "__main__": # open text file to read and write file = open('write_file.txt', 'r') # set the pointer to the desired position file.seek(5) data = file.read(1) print(data) # rewind the pointer file.seek(5) data = file.read(7) print(data) file.close()
The preceding code can be explained in the following steps:
Read now
Unlock full access