You are on page 1of 2

11/10/2017 python - seek() function?

- Stack Overflow

Learn, Share, Build


Each month, over 50 million developers come to Stack Overflow to Google Facebook
learn, share their knowledge, and build their careers. OR

Join the worlds largest developer community.

seek() function?

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.

python

asked Jul 27 '12 at 22:28


Gmenfan83
782 3 17 27

3 you may consider to accept the answer with better explanation and more karma Peter Masiar May 18 '16
at 16:11

2 Answers

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.

edited Jul 27 '12 at 22:43 answered Jul 27 '12 at 22:38


Nicols
5,554 4 25 38

60 +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

When you open a file, the system points to the beginning of the file. Any read or write you do

https://stackoverflow.com/questions/11696472/seek-function 1/2
11/10/2017 python - seek() function? - Stack Overflow
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), reads one byte, repeat.

answered Jul 27 '12 at 22:34


DGH
5,878 1 15 24

11 -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

https://stackoverflow.com/questions/11696472/seek-function 2/2

You might also like