Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Data python 3
  • Overview
  • Transcript

3.3 Navigating a File

Have you ever started to skim through sections of a book or an article looking for specific pieces of information? It wouldn't make sense to start at the very beginning if you knew roughly where you needed to look. Python allows you to do the same thing. In this lesson, I'll show you how to seek a specific location in an open file.

3.3 Navigating a File

Over the last couple of lessons we've been working with the contents of a file. We've been opening the file. We've been reading the entire file. We have been grabbing it in one large chunk in a string. We've been splitting it up into a list. We've been processing it one line at a time by iterating over it using a loop. And these things are all great, but we're doing things in large chunks. What if we don't wanna do them in large chunks? What if we wanna be able to go through a file and pick and choose and grab just certain parts that we want? And you can absolutely do this. And I'm gonna show you how to do it now using a couple very powerful functions built in to Python and dealing with files. So what we're gonna do here is, we're actually going to be using the interpretive shell. So I'm going to open up the interpreter so I can be interactive here, I can interactively work with this. Now I still have my lorem.txt file there, so let's go ahead and grab that. So we're going to say lorem_file is going to be equal to open, and I wanna open lorem.txt, just like we've done before. Now, before you start doing anything with this file, let's start to think about how lorem_file is holding a representation to that file. So when we initially open up that file like this, we have access to that file. But there's something that's keeping track of our current position within that file. So how do we know where we are within that file? Well, it's pretty easy, there is a function on this file known as tell. Now, what tell is going to do is, it's gonna give you back your current position within that file. So by default, you start at the very beginning of the file at index 0. You are at the zero byte of the file, you're at the very beginning, you haven't done anything yet. So what happens when we read the file? So let's say I wanna read the entire file, like we did last time. I'm gonna use the read function. It's gonna spit back the entire file to me. Now, if I were to run my tell again, you're gonna see I am now at index 1383, or the end of the file. So what does that mean, and who cares? Well, if I were to go back and run read again, you're gonna see, I get back nothing. And the reason for this is because the read operation is uni-directional or unidirectional. I can only go from the beginning to the end, and once I'm at the end, I'm done. But that seems a little limiting, I don't wanna restrict myself to that. What if I need to go back and read it again? What if that file is gonna change, and I need to go back and read it again? Or I wanna go back and read the beginning of the first sentence, or I wanna grab the third line or, something like that. How do I do that? Well, there is another helper function that's built in here called seek. And what seek is going to allow us to do is to specify a location within the file that I want to go back to, so let's try that. Let's say lorem_file.seek, and I'm gonna specify a location here of 0. I wanna go back to the beginning of the file and start all over again. So now it returns back what index we are at again. So if I were to use tell again, you'll see that I am truly back at 0. And if I run read again, I'm gonna get the entire contents of the file. And once again, now if I say tell, now I am back to the end again at 1383. So that's pretty good. And I know how to determine where I am in the file by using tell. I can read, or I could use read lines, or I could do whatever I wanted to do, any of those read-type functions. And then I could use tell again and find out where I'm at. And then I could move back somewhere, I could go back all the way to 0, or I could go back somewhere in the middle. So I could do seek, and I could say instead of going to 0, let's go to 500, somewhere in the middle of the file. And then if I were to do read again, you're gonna see I'm only gonna get about half the file, because I went back to index 500 and then read forward everything else. And this is all good, this seems to be working out oka. But what if I don't wanna go that far? What if I don't wanna read everything? What if I wanna be a little bit more selective? All right, well, let's reset ourselves back to the beginning. So I wanna go back and I wanna seek back to 0. Let's start at the beginning and let's be a little bit more choosy. Let's go ahead and move just a little bit ahead into the contents of our file. Let's move forward 12 spaces. So we're going to go to the 12th byte of the file by using seek, I'm gonna move ahead to the 12th location. Now remember, in seek you are specifying an offset, a byte offset of where the file you want to be. So I wanna move ahead to the 12th byte, so now I am at number 12. And that's gonna put me roughly past lorem ipsum up to the third word here. So that's roughly where I'm going to be. What if I just wanna read that word? Well, in this case I know there's five letters and I know there's five bytes on that. I wanna read just that one, I don't wanna read everything. So let's try that. I could say lorem_file.read. Now read also takes a parameter of how many bytes I wanna read. So similar to seek, seek takes in the index of where I wanna go to. read can specify the number of bytes I wanna read, so I wanna read five. So as you see here, it gives me back dolor, now that's great. And now I've moved ahead ,so I can say tell, so I can say where am I? I am now at the 17th byte, so I've started at 12, I read 5, and I've moved that pointer up 5 spaces, I am now at 17. So if I were to read five again, I'm now going to move up a little bit further, and that's great also. But we're a little bit restricted here, and if you've been able to determine that, then congratulations for you. If you're not seeing it yet, everything that we're doing here is absolute. So we seek to an absolute position. We read a predefined number of bytes. We use tell to say, absolutely within the file, where am I? None of this stuff is relative, so how can we do that? Is there a way to maybe just move ahead another predefined number of bytes and then read some more? Or do I have to calculate exactly where I need to go and put myself out there? Well, there's really not a way to do that explicitly. But we can piece these things together and combine these pieces of functionality to figure out how we can do that. So in order to relatively move in a certain direction, we're going to use lorem_file, and in this case I want to seek. Now remember, seek is going to take in the absolute location of where I wanna go within my file. Well, we could be a little bit more selective of that. So if I wanted to move ahead six spaces, if I wanna move ahead six bytes, well, I could figure out where I am currently and then simply add six bytes to that. Well, we can absolutely do that as well. We could say lorem_file.tell, so give me where I am right now, and then let's add six bytes to that. So let's try that out. So now I am actually at location 28, I'm at the 28th byte. And now I could read again, I could say lorem_file.read, and this time I wanna read 12 bytes. And there I get back another word. So as you can see, I can be a little bit more selective in where I wanna go within my file, and where I am located currently and figuring out how to get there using tell. I can then go to a certain location within my file using seek. And I can use combinations of those things to move absolutely and relatively. And then I can read just certain sections or pieces of my file using read, and specifying the number of bytes that I want to read. So as you see here, you can be a little bit more pickier, a little bit more choosy in what you wanna read, and you're not just limited to reading the entire file in large chunks.

Back to the top