How does Python's seek function work?

jlconlin picture jlconlin · Nov 7, 2012 · Viewed 15.1k times · Source

If I have some file-like object and do the following:

F = open('abc', 'r')
...
loc = F.tell()
F.seek(loc-10)

What does seek do? Does is start at the beginning of the file and read loc-10 bytes? Or is it smart enough just to back up 10 bytes?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Nov 7, 2012

It is OS- and libc-specific. the file.seek() operation is delegated to the fseek(3) C call for actual OS-level files.