In this video, Kathryn teaches you about seeking: What it is, how to do it in Python, and why it is needed for reading a given file multiple times.
[Instructor] - Before going more into files, let's take a look at what it means to seek. In all files, we have a seek pointer that points to a specific character in that file. When we read or write, the seek pointer moves. Ultimately, it keeps track of where we are in the document, and before reading or writing, it will be set to zero. Let's say, in this file, we want to write H-A-P. When the command executes, the program will first input the H into the file. After writing the H, our pointer will move from zero to one.
Next, the program will write the A in our H-A-P, and our seek pointer again will move from one to two. Lastly, we'll write a P to finish off our H-A-P, and our pointer will move from two to three after writing this letter. We've written all that we wanted to in this file, but now let's read it. In order to read it, we must set the seek pointer back to zero, and go through the file again. We can do this with a function called Seek, or we can close and reopen the file in our Python program, which will reset the seek pointer for us automatically.
Either way, say the seek pointer is back at zero and we want to read the whole line we just wrote. The program would first read the H, and after reading the H, the seek pointer would move to one. Then the program would read the A, and move the pointer to two, just like before. And eventually, the program would read P, and move the pointer to three, and the whole file will have been read by the program. So what's important here? The seek pointer keeps track of where we are in the file. Both reading and writing move the seek pointer, and if we want to read the file after writing or read the file multiple times, we must set the seek pointer back to zero; either by running the Seek function, or closing and reopening the file in our code.
In the previous video, we were looking at the scores, and we read the scores once, and we had this output, which was from the beginning of the file, and then we read again and we had this, but that was from a different part of the file. This is because when we read it first, we were reading from the beginning of the file, but when we read again, we hadn't reset the seek pointer. And so to reset the seek pointer here, we can just go "myfile.seek" and we can set it to zero. And when we run this again, they both start at that beginning point.
Another way we could do this is by closing and reopening the file. Instead of this, you could go "myfile.close" and then you could just copy and paste this line. And this will do the same thing as doing that one line of code where you use the Seek function. And so running this, they both also start at that first point in the file with that first score.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content