We just saw that the file obsect keeps track of where we left off reading in a file. What if we want to move to a previous location? For that we need the tell () and seek() methods. Here's an example that; continues our read ( ) example:
>>> f.seek(0) #reset the file pointer to 0
>>> f.read() #read in the whole file '012345678 9ABCDEF'
>>> f.tell() #see where the file pointer is 16
>>> f.seek(8) #move to the middle of the file >>> f.tell() #see where the file pointer is 8
>>> f.read() #read from the middle of the file to the end '8 9ABCDE F'
The second line reads in the whole file, which means that the file pointer was at the end. The third line uses tell () to report where the file pointer was, and then seek() positions the pointer to the middle of the file. Again,te ll() reports that location. To demo astrate that read() picks up from the file pointer's location, we'll read the rest of the file and display it.
Was this article helpful?