Hi, I am having a little trouble with the seek function. I can’t understand it. I looked up some explanations on the web but I still have a little difficulty. Like in ex20, how does seek know we are on the next line since it deals with bytes, right??? Any explanation would help a lot.
seek doesn’t know where you are in the file. It only goes to a position. When you want to know where you are, you need the command
Seek tells the read function where to start from. To set the position, you use the seek() function. It is used in the form
# go to the beginning of the file seek(0) # go to 45 bytes/letters after the beginning of the file. seek(45)
More examples like this and a good explanation of the two parameters
whence you will find here: http://sthurlow.com/python/lesson10/
Another good explanation of what seek does.
Read the code examples carefully.
Sorry for the many links, but they did a very good job at explaining it, so I don’t need to
And here ausefull excerpt from python.org. A little bit complicated, Python docs doesn’t come easily (I had to read it a couple times).
seek ( offset[, whence])
Set the file’s current position, like
stdio's fseek(). The whence argument is optional and defaults to
0(absolute file positioning); other values are
1(seek relative to the current position) and
2(seek relative to the file’s end). There is no return value. Note that if the file is opened for appending (mode
'a+'), any seek() operations will be undone at the next write. If the file is only opened for writing in append mode (mode
'a'), this method is essentially a no-op, but it remains useful for files opened in append mode with reading enabled (mode
'a+'). If the file is opened in text mode (without
'b'), only offsets returned by tell() are legal. Use of other offsets causes undefined behavior.
tell ( )
Return the file’s current position, like
I’m curious, have you ever used an old school tape deck player? Like a cassette tape player? Or a CD player? DVD player too? TV remote?
The Fast Forward and Rewind buttons used to be called “seek” buttons, so that’s where it comes from. At least that’s what I remember them being called. A file in Python is actually modeled on this kind of tape because originally files were literally stored on tapes. Not kidding. Google up some old mainframe photos to see the tapes.
Now we don’t have to actually seek forward and backward linearly because we can just jump around randomly in files, but people still use that word for what should be “goto”, or “reposition”, or “move”.
@DidierCH’s explanation is right, but usually this explanation of why that word exists for this operation helps you remember what it’s doing. It’s taking a “read head” that is at a spot in the file, and seeking it to a new position. The positions are then given as integers by the number of characters from the start. Seeking to the start is just seeking to 0. Seeking to the end is just seeking the length of the file.
That’s a great explanation that creates some images in the head. A really nice mnemonic. It’s more likely to be understood as my technical abstraction Thank you for this. We need more of that. It would be easier to learn programming.
I find that using real world things people already use and telling them the history of why it is that way, helps them remember it.
Thanks, that really helped a lot!