How does Python's seek function work?

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


According to Python 2.7's docs:

file.seek(offset[, whence])

Set the file’s current position, like stdio‘s fseek(). The whence argument is optional and defaults to os.SEEK_SET or 0 (absolute file positioning); other values are os.SEEK_CUR or 1 (seek relative to the current position) and os.SEEK_END or 2 (seek relative to the file’s end).

Say you would want to go 10 bytes back relative to your position:

file.seek(-10, 1)

It should be smart enough to just back up 10 bytes, but I suppose that the details really depend on the filesystem/OS/runtime library you're using.

Note that if you just want to back up 10 bytes, there's no need for tell.

F.seek(-10,1)

Tags:

Python

Seek