151

Please excuse my confusion here but I have read the documentation regarding the seek() function in python (after having to use it) and although it helped me I am still a bit confused on the actual meaning of what it does, any explanations are much appreciated, thank you.

0

4 Answers 4

263

Regarding seek() there's not too much to worry about.

First of all, it is useful when operating over an open file.

It's important to note that its syntax is as follows:

fp.seek(offset, from_what)

where fp is the file pointer you're working with; offset means how many positions you will move; from_what defines your point of reference:

  • 0: means your reference point is the beginning of the file
  • 1: means your reference point is the current file position
  • 2: means your reference point is the end of the file

if omitted, from_what defaults to 0.

Never forget that when managing files, there'll always be a position inside that file where you are currently working on. When just open, that position is the beginning of the file, but as you work with it, you may advance.
seek will be useful to you when you need to walk along that open file, just as a path you are traveling into.

5
  • 131
    +1 for explaining the second parameter, although I'd like to add that you should probably use the constants os.SEEK_SET, os.SEEK_CUR, and os.SEEK_END instead of the magic numbers 0, 1, and 2, respectively. Nov 11, 2014 at 17:16
  • The seek position is a byte index into the contents of the file similar to an array index. Its also interesting that if we open file in append mode 'a', we cannot seek to file's beginning.
    – anilmwr
    Apr 24, 2018 at 7:06
  • 3
    Actually in case of append mode, we can seek to file's beginning using fp.seek(0, 0), but as soon as you try to write to that file, seek will again reset to file's end and contents will be written at the end.
    – anilmwr
    Apr 24, 2018 at 7:13
  • 1
    Thanks - learned something new and only had to read this clearly explained answer onces Feb 18, 2020 at 15:40
  • when calling seek from the beginning of the file, does it actually start 'seeking' from the beginning of the file each time? May 29, 2020 at 9:59
48

When you open a file, the system points to the beginning of the file. Any read or write you do will happen from the beginning. A seek() operation moves that pointer to some other part of the file so you can read or write at that place.

So, if you want to read the whole file but skip the first 20 bytes, open the file, seek(20) to move to where you want to start reading, then continue with reading the file.

Or say you want to read every 10th byte, you could write a loop that does seek(9, 1) (moves 9 bytes forward relative to the current positions), read(1) (reads one byte), repeat.

1
  • 15
    -1: This answer fails to explain the second parameter, and actually, as phrased, makes it sounds like the second parameter dictates how many bytes are read. Nov 11, 2014 at 17:15
3

The seek function expect's an offset in bytes.

Ascii File Example:

So if you have a text file with the following content:

simple.txt

abc

You can jump 1 byte to skip over the first character as following:

fp = open('simple.txt', 'r')
fp.seek(1)
print fp.readline()
>>> bc

Binary file example gathering width :

fp = open('afile.png', 'rb')
fp.seek(16)
print 'width: {0}'.format(struct.unpack('>i', fp.read(4))[0])
print 'height: ', struct.unpack('>i', fp.read(4))[0]

Note: Once you call read you are changing the position of the read-head, which act's like seek.

2

For strings, forget about using WHENCE: use f.seek(0) to position at beginning of file and f.seek(len(f)+1) to position at the end of file. Use open(file, "r+") to read/write anywhere in a file. If you use "a+" you'll only be able to write (append) at the end of the file regardless of where you position the cursor.

2
  • Are you talking about files or strings?
    – RalfFriedl
    Nov 10, 2019 at 21:22
  • Sorry for the confusion. It's all about files. Nov 11, 2019 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.