 Welcome back everyone. This is Brian. We're going to talk about reading a text file. The main takeaway from this is it is easy. Anybody can do it. So if you're intimidated with the file system, do not worry. This is super ridiculously simple. Let's dive in and take a look. First things first, let's go ahead and get a file name. Now your operating system's full of files, but we want to read a file and not disrupt any other application. So we're going to read ourselves. Meaning this file right here that you see on the screen. Ta-da! We're going to read this. So we have to get the file name to the script. How do we do that? Well, there's a couple different ways. We're going to show how to do a few of them. So we're going to say OS and Sys, we're going to import both those modules. You do that just by putting a comma. We could have done it one per line with import module, import module. I'm just going to do it all in one shot. Let's go ahead and say prints. And you're going to see this quite a bit out in Internet lands. Where someone is going to say something like this. Whenever you see the double underscore, it's kind of like a built-in little piece of information in Python. It gets a little more complex than that, but just understand that double underscore means built in. And you can see, ta-da! We've got the full path to our script. Now there is a problem here. Now the problem is that may not always exist. Sometimes you're going to get into a situation where this just simply does not work. So again, going back to our previous conversation, there's always multiple ways to do something. So we're going to show you what I believe to be a better way. And we're going to use the arguments. Now, args or arguments are usually not a good thing. Usually you want to avoid getting into an argument. But in this case, we want to dive right into them. So we're going to just print out the sys and we want the argv. What's going on with this? Well, under the hood, when Python calls your script, this guy, Python, da-da-da, it actually sends you a list of arguments. You ever like, in your file browser, double click a file and your operating system knows what program to open it up in? Those are arguments. So it will call your script. And there'll be another, it'll be like comma and then whatever you want to work with. So just know the first argument Python's going to send you is, well, the name of yourself, what you're currently running, makes it super easy. Now, let's go ahead and say, we want to get the full path here just to be sure you want the OS path, we want the ABS path. Now, a lot of folks in the comments are going to say, why are you using the ABS path or the absolute path? Why don't you use this other one? We're just doing this for demonstration. We haven't really dived in the complexity. I'm going to do an entire video on the different types of paths and what they mean and which one's best and so on and so forth. But right now we're still in newbie lands. We're just going to do the ABS path. And then we're going to print out reading whatever this file is. Let's go ahead and run this. And there it is. So it's just simply handing us back a list of arguments. The first one or the zero position is always the script that's currently running. One thing I really want to demonstrate is, does it exist? Well, if we're running our own file, we know it exists. If we're going to run this and read it, we know it exists because we're currently in there editing it. But just for giggles, I want to cover how to determine if the file exists. I mean that is the existential question. Does the file exist? Do we exist? Interesting. So anyways, let's say if and let's go ahead and say not OS path. Exists, you may be wondering what not is not is similar to something like not equals to it's kind of short version for that. But I'm just going to say, if not OS path exists. And then I want to give it the name of our file, which we got up here. So if it does not exist, then I want to say, Hey, print. And let's go ahead and print that file not found. Because if we try to work with some file that doesn't exist, we are going to get an error message. And what we're going to end up getting is something like this where it's just going to say file not found error, which would be covered later on. So if you're just churning away and you give it the wrong file name, you're going to have a bad time. But now that we know the file does not exist, we don't want to go any further. So I'm going to say exit. And I have to give it some sort of exit code. And I'm just going to say one case you curious, a normal exit is zero. And then you would give it some sort of error message like access denied or whatever, but usually vendors will have different exit codes. So I'm just going to say one, just so we can tell the end user, Hey, something bad happened. But because we're reading our own file, it should work just beautifully. So let's run this. Sure enough, works beautifully. Let's go ahead and take this and let's give it a name I know just simply is not there. Nope.txt. Let's run this again. File not found and then it exits out. Works as expected. Now comes the confusing bit. We're going to actually start working with the file. We're going to start with opening the file. And if you get a file not found error during this, it means you have the wrong file path and you did not follow the directions. Or something's going horribly wrong with this. So what we're going to do is we're going to say F equals anyone call open. Sounds ridiculously simple. And it is now we're just going to give it a path. Now comes the confusing bit. We have to give it what's called a mode. Now I'm going to do another video on modes, but this one's just going to cover reading text file. So I'm going to say are just stands for read. And we're just going to read plain text. But what this whole thing's going to do is it's going to go out in your operating system. Make sure that file is there. If not, you're going to get a file not found error. And then it's going to try to open it for reading. And it's going to put the cursor in the file at the zero position. It's going to hand you a file object back. So this one little line, there is a lot of complexity going on there. Now that we have the file open, let's go ahead and read a line. And I'm going to say the line equals we're going to take that file variable. And I want to say read. And you notice how we get a whole lot of options. Read, read a bowl, read the line and read lines. We want read lines. So we're just going to read one single line. Let's go ahead and print that out. Save run. And ta-da. Reading a text file. So read the first line out of our file. Very, very cool. Also notice how it put this little return down here because guess what? There's a return right there. At the end of this, there is an invisible slash r slash n or slash n, depending on your operating system. Just know that it's also going to include that. So when we print this out, we're going to have to strip that out. But we have successfully read a line. Now if reading a line was cool, you're going to really love this. We can actually control this. So I'm going to read a number of letters. Here's equals f dot read. And notice how it wants a number. So we're going to read 10. Let's go ahead and print this out. And I'm going to actually format this. Save here's and we'll put star star. And then what we're actually reading. So if you look at our file here, it says reading a text file. And now the cursor's right here. So let's run this and see what happens. And you see how it says cares star. And then it's got a return line feed, get a f I and then star. So we've actually controlled how many characters we've read. I wanted to do it this way to really show the complexity here. Notice we've got this hard return line feed here. We may not actually want that. So if you're using the read function, you're going to get some weirdness like that. And that's why I tend to use read line when I'm working with the text file. Now somebody very observant is going to notice as we've been reading, things have been moving forward, meaning we read a line. Then we read a number of characters. And it said get a fi. It didn't start back over at reading. So what's happening here as we read, there's an invisible cursor, much like in this text document, you see that cursor's right there. It always starts off at the zero. So invisibly, there's a zero right there. And it's just like a list or a tuple or anything like that. It's a zero based index and it's going to start at zero. And as we read, it will move forward. So as it says read line, it's going to move there. And then when we say, read 10, it's going to there. So it's going to actually count. So this invisible cursor is going to keep moving. What I want to do now is just show the position. How do we figure out where we actually are in the file? This is a bit advanced and it may not make a lot of sense to you in beginner land, but as you'd get into advanced file processing, you're going to need to be able to move back and forth in a file. So I want to introduce this concept fairly early. Let's go ahead and say print. And we are going to format this and say position. And I wish they had named it position, but instead they named it tell. So we're going to call the tell function, which is going to tell us the position. Now I want to say of, because I want to know how big this file actually is. So I'm going to say OS. And we're going to call the statistics. We're going to use our S file variable that we have earlier. And I want to know the ST underscore size. And if that's super confusing, sometimes intelligence will help you out. If you're working with an advanced telescope system like kite or something like that. But you can dig through the documentation or you can just, you know, trust me, OS dot stat file name ST underscore size. So we're getting back in integer. Now the size is the size of the file in bytes. Let's run this. So we are position 31 of 529. And as we add more and more to this, you'll see this position change. Even though it's a comment, the file is growing in size. Let's run that again. This year enough 538. Remove that and 530 because we deleted some stuff. So that is how you would determine where you are and the file size. Major takeaway here is as we're reading that invisible cursor is moving. Now I want to put it right back at the very beginning. So we're going to call seek. And again, this is more of an advanced function, but I want to introduce it very early. This is always a zero based. Remember our conversation about lists and tuples and dictionaries. It's always a zero based index. So I'm going to say our file object dot seek. And we're going to seek to zero. Now, if you caught that as I was typing, you see this seekable. Some files are not going to be seekable. So that may break if you're doing some custom work. But for our purpose, it should work just fine. We're just going to tell it where to seek. So seek zero is going to tell this move all the way back up. So if we were working and we're just reading, reading, reading, reading, reading, a seek zero is going to say go right back up to this zero spot. Let's run this. And graphically it has no effect. But if we do another read, you'll see it actually start over again as we're about to demonstrate. So now that we understand this invisible cursor and positioning and we know how to control where it is and we've set it back to the zero position. Let's go ahead and read all the lines. I'm putting this big long line here. I'm going to print this out just to separate all this out so we can see it in action here. What I'm going to say is for L in F dot read. I want read lines. Notice there's read line, which will do a single and read lines, which will just keep going and going. And we're going to read this entire file line by line. I want to say print is going to hand me back a string. So I'm going to say L, we want to strip that. Now what strip does, it removes any extra white space. So those hard returns, like right here, how we got these little returns, it's going to strip that out. If we don't do that, we're going to get some really funky results and I'll demonstrate that. So let's run this. See, stopped it right here. So here's the start of our file and it went all the way down to where we are. Pretty cool, huh? Now if we comment this out and just grab this. This is what I mean by you'll get some funky results. If you don't strip that out, because print automatically puts in the carriage return line feed. Let's go ahead and clear all of this out. You can see how now it's got this extra space in between each line. So that's why we need to call strip. Let's go ahead and put this right back the way it was. And ta-da, it just works. Okay, systems resource one on one. Anything you open, you have to close. So we are going to now close that file. Think of a file like a door on a really cold day. You open that door, you're letting all that weather in. Now you want to close it. So what close does is a few different things, but for our purposes, it's very simple. It allows other applications to work with it. If we have opened this and the underlying operating system has locked the file for reading, that means nobody else can make modifications to it. Don't worry, ridiculously simple. F close. Always, always, always, when you open a file, close it. You don't want what's called a hanging resource. Now, Python can and most of the time does close the file automatically when your script's done, but don't count on that. Sometimes you'll see out there, if you open a file and forget to close it, it just stays open forever. Very, very frustrating. So close. You're not going to see anything graphically changed, but it is now closing that door so other people can open it and do what they want to do. Quick recap. This video, we have covered the complexities of the file system for reading a text file. Now, text file is different than a binary file, which we're going to cover in a future video. So we're talking about just plain text. We talked about importing. We talked about getting the current file, determining if it exists, opening a file, reading some lines and letters and all the lines, moving around in the positions and how to actually close the file. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the Voidrom's Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on Udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.