 Hey everybody, it's Brian. Welcome to the 15th Python tutorial. Today we're going to be discussing reading text files. And this is actually a pretty common thing you're going to have to do throughout your career as a programmer is read a text file or specifically read it one line at a time. So we're going to show two techniques here. One, how to read the entire file and help if I spelled import correctly. So to read the entire file we should probably actually, you know, do a little bit of error checking here. And I'm not going to go through making a function with the tri-exempt, finally, because we've done that before. I just want to very quickly do this and show you how it works. So we're going to say, if we'll call it spath, let's actually make my variable file name here, the file we've been working with. And you can use just pretty much any file, but it should be a text file. If you get a binary file, then we'll be covering that in future tutorials. But the difference between binary and text is a text has things that's human readable, where a binary just has a bunch of numbers in it. So like a program, well like PyCharm that we're working with would be a binary file where an email would be an example of a text file. So if we're going to say, if it exists, say with open spath as f. Now what the width command does is it says take an object and with that object do something. So we're saying with the open object or the f variable more in this case because we're returning something from that. Is that confusing? Let me explain that a little better. With an object, we're going to do something. So with and then this function is returning an object, we're going to call f. So with f, we're going to do something. Should be a little clearer when I do this. When we read the entire file, we're just calling read and it's going to read everything into memory now. That can be good and that can be bad. It can be good if it's just a small file and you want the entire contents to it. See, there's the entire contents of this file. It can be bad if you have multiple lines and you want to do something for each individual line. And there's really, you know, with any language, multiple ways to do this. And we're going to just grab that, make sure it exists. Otherwise, if it doesn't, it will give you an error message. And what we're going to do here is we're going to get all of the lines. So what we're going to say is f.read lines. Now when we run this, you'll see it returns this nice list. The problem with this list is it's got these slash ends in there. So it's got a new line in there and we'll have to actually strip those out. And you would do that by, let's actually call that line and lines. And let's back up here a little bit here. Lines equal f. You know what? Let's just do it the easy way. So for f and read line, or for line and read lines, we're going to print. We're going to strip that line out. We're going to strip out the escaped character there. Let's actually put something in here so we can see that we're doing something different. So we're going to read the lines and you can see how we're stripping them out. If we don't, if we just print the line itself, it's going to look a little weird. And let's demonstrate that. You can see how it's got these new lines in there because we didn't strip out that new line character. All right. So and there's an even better way of doing this. Reading the actual lines one line at a time. So what we're going to do here is we're going to say do this and we'll say line equal f.read line. And this is just going to read the one line out of there. And we'll say while line, meaning while we have a line object, remember everything in Python is an object. So if it's nothing, it's just not going to execute this code. Remember, we've discussed the while loop in previous tutorials. We're going to print this out. We're going to say line equals, and then whatever processing we were going to do to this. And let's actually throw that here. We'll say line. And we want to strip out any white space if there's, you know, like extra spaces before or after we want to strip those out just because. And then of course we want to say line equal f.read line. And what this will do is it'll advance to the next line. So let's discuss this a little bit here. Actually, first let's run it, make sure it actually works. Yeah, there's a problem. Okay, indentation. Gotta love it. All right. There we go. So we're reading the lines one line at a time. And you can see how this is line equal. And then it's got our line. And we've stripped out any white space. I know it doesn't look like it. You see that little blank space that's actually added by pie charm. So what we're doing is we're saying if it exists with open as f, meaning we're going to call the open function and return an object called f, we're going to read a line. If that line object exists because read line will return none if it's just nothing. It will, you know, if we get that object, then we're going to go this while loop while line is true. Basically, we're going to print it out. And then we're going to get the next line. That's a mouthful. So those are three handy ways to read a text file. I typically get inundated with, Hey, how do I read a file one line at a time? Well, that's probably the best way to do it. As with any programming languages, there's multiple ways to do it. But I think that's one of the more efficient ways. You should know if there's really no air handling in here. So you'd have to encapsulate this within the tri block. That's all for this tutorial. Hope you found this educational entertaining and feel free to visit my website void realms.com. Bring that up here void realms.com for the source code for this and other tutorials. Just go to tutorials and then Python and join the Facebook void realms group. There's over 200 of us in there that are willing to help