153

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.

Gmenfan83
  • 2,487
  • 5
  • 30
  • 34

4 Answers4

268

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.

knia
  • 463
  • 6
  • 16
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
  • 133
    +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. – ArtOfWarfare Nov 11 '14 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 '18 at 07: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 '18 at 07:13
  • 1
    Thanks - learned something new and only had to read this clearly explained answer onces – Adam Hughes Feb 18 '20 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? – Theo Stefou May 29 '20 at 09:59
47

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.

mercator
  • 28,290
  • 8
  • 63
  • 72
DGH
  • 11,189
  • 2
  • 23
  • 24
  • 16
    -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. – ArtOfWarfare Nov 11 '14 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.

user1767754
  • 23,311
  • 18
  • 141
  • 164
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.

Xavier Nicollet
  • 353
  • 6
  • 12