For random-access devices such as disk files, there are methods that let you find your current position within a file, and move to a different position.
returns your current position in file F.tell().
F
moves your current position
to F.seek(N), where
a position of zero is the beginning of the file.
N
moves your current position by a distance of F.seek(N, 1) characters,
where positive values of N move toward the end of the
file and negative values move toward the beginning.
N
For example, would move the
file position 80 characters further from the start
of the file.
F.seek(80, 1)
moves to a position F.seek(N, 2) characters relative to the
end of the file. For example, N would move to
the end of the file; F.seek(0, 2) would move
your position to 200 bytes before the end of the
file.
F.seek(-200, 2)
>>> treeFile = open ( "trees" ) >>> treeFile.tell() 0L >>> treeFile.read(6) 'yew\noa' >>> treeFile.tell() 6L >>> treeFile.seek(1) >>> treeFile.tell() 1L >>> treeFile.read(5) 'ew\noa' >>> treeFile.tell() 6L >>> treeFile.seek(1, 1) >>> treeFile.tell() 7L >>> treeFile.seek(-3, 1) >>> treeFile.tell() 4L >>> treeFile.seek(0, 2) >>> treeFile.tell() 26L >>> treeFile.seek(-3, 2) >>> treeFile.tell() 23L >>> treeFile.read() 'er\n'