 Welcome back, everyone. My name is Brian and we're going to continue our journey in a Python 3 and we're going to talk about how to write a text file. This is plain text, not binary. We're going to talk about writing binary files in a future video. We're going to take what we've learned in the previous video and overly simplify it using functions. The last video was kind of verbose. I want to make life much, much simpler. Our first goal is to simplify mode usage. Now, what are we talking about here? We're going to cover two different modes. We're going to talk about write and append and they do two totally different things, but we can use the same code. So let's go ahead and make a function. I'm going to say to file and let's go ahead and save file name, mode, data. I hear a lawnmower behind me. I apologize if the microphone's picking that up. It's just kind of COVID-19. Nothing you can do about it. My neighbor likes to mow his lawn 100,000 times a day. So I'm going to say open and open that file name. And we want the mode. Now remember in the last video, we opened it with the read a text or the R mode. We're going to work with W and a, but they can work very, very similarly using the same code. As we're about to demonstrate, I'm going to say for range. And we're going to go ahead and say range. Let's just pick a number five. And let's go ahead and write this out. And I want to say the string representation of that number. You're going to get some sort of error message if you don't do that. Go ahead and load colon in there along with the data. And let's go ahead and add in a carriage return line feed slash r slash and those are our escape characters. We talked about that when we're in string land. So really all we're going to do is we're going to write out the string representation of the number using str to convert it from an integer to a string. And then we're going to add a colon, whatever data we're in, and then R and data is going to come to our function as an argument. Now, there exists this tendency to do something like this and you're going to see this flush. I'm going to comment this out. What does flush do and why do people have it in their code? When you write to a file, you have an invisible buffer in the background. I should say Python does and that buffer will fill up because you can add more to it faster than it can write down to the disk, especially if you've got a slow hard disk. Think about like embedded systems, how they get really slow. You don't really need to call flush. If you're going to close the file when you're done with the file you close it. Python calls this automatically when it's closed. However, if you're doing some sort of like sequential writes or something like that and you want to just write to the file in chunks, you're going to want to flush it periodically to make sure that data gets flushed down to the disk. I wanted to put that there because I know somebody's going to ask, why didn't you flush the contents? You don't need to because close calls that automatically. I hate that name, but at the same time it makes me chuckle because you're going to flush it. I always think about looking down as disgusting as it is, looking at a toilet. You're just going to drop data into the toilet and then flush it down. That's literally what that does. It flushes all of that buffer down into the hard drive. We're going to cover two modes. The first one is write. When you hear the word write, write will overwrite, meaning it's going to take the existing file, completely erase it and start from scratch. You need to be a little bit careful when you do that because if you have something sensitive or something you wanted to save, it's just going to obliterate that data. It's just gone. It will not be in your recycle bin. It will be gone forever. You need to be a little bit careful here. We're going to write file and let's go ahead and say, I need some special variable name like file name. There we go. We're going to call this to file function. We have a file name, mode of data. Let's just go ahead and grab this. We're going to call our function here. So we're going to reuse the variable file name, mode. We're going to switch this to W for write. And the data we're going to say hello. Remember, write will overwrite. It will completely destroy that and start over. The other mode we're going to talk about is append. Now append will add. So write will overwrite append will add. Now what this does is it opens the file seeks. Remember, we talked about seeking moving through that file. It's going to go to the very end of the file. And then it's going to start adding to it. Now this is what I mean by simplify mode usage. We can literally take this and just paste it and then simply change this to an A for append. And then let's change this to hello again. So what's going to happen here is we're going to call write. It's going to obliterate the file start over and then up to five, it's going to say hello world. And then we're going to call append and it's going to add to it. All right, the immediate goal of this was to write and append, but I want to read this file back. So I want to use an import. And I'm going to put it all the way at the top of file. This is not mandatory, but it's almost like an industry standard, if you will, to put your imports at the very top rather than just adding it right here, which you actually could. But let's put it all the way at the top. That way, anything that we've just kind of flubbed through and we've used OS will get it as well. And if we decide to go back and modify these functions and use, we don't have to accidentally import it multiple times. So in short, always put your imports at the top of a file. We're going to say def and let's go ahead and read file. And I need some special variable name something that really stands out like I don't know, file name, we could name it fuzzy button kitten or whatever we want. All right, so we're gonna say if not, we want OS. And let's go ahead and say path, not exist, we're just going to make sure the file we're trying to read actually exists. Otherwise, we're going to get some bad, bad errors. Now, instead of exiting, I just want to return right out of this function, not kill the entire script. Now I want to say, we're going to open this up. And this is how just ridiculously simple working with files and Python can be. Give it a mode. And because we've opened something, let's go ahead and before we do anything else, call close. Now in between open and close, we can do whatever we want to do. For example, I could say for a line in F dot read lines, like this, or if we wanted to really, really, really oversimplify that we could just read the entire file in one line. And we're just going to call. Now, spoiler alert, if you do that on a very large file, you're going to have a very bad time because you may run out of memory. Your little app may crash. For example, if you have three gigs of memory, you try to read a 30 gig file, it's just not going to do it. And it'll sit there for three hours before crashes, but it'll eventually crash. That's why we use things like four line in read lines because it reads it one line at a time, not the entire thing. Okay, our warning about reading all of it in one function. If you had a massive file, notwithstanding, we're gonna leave it like this just for demonstration purposes, because we're going to work with a very small file, we're going to see this in action. I'm going to say my file, we could name is really anything we want. Let's name this hello dot txt. First things first, we're going to write this file. Just going to call our function. Now let's go ahead and append the file. And let's go ahead and read the file. There's a reason why we're breaking this up into functions and it's so we can reuse this code, you can hear that term a lot in programming code reuse, rather than hard coding the file name and then calling or I should say rewriting this function for every single file is just simply to use a variable and reuse the code. So let's go ahead and run this. And you can see how it says zero through four hello world and then there are append kicks in here. Hello again. Now let's demonstrate this. So let's get rid of this append. And let's just do right. Notice how we already have a file out there and we can actually open it up and this is exactly what it looks like. Let's go ahead and get rid of that append and let's see what I mean by right file will overwrite. This is why it's so dangerous. Hello world zero through four and if we open our file back up you can see hello world all of that hello again is gone because it obliterated that file and rewrote it. Now we can do this. And it goes right back the way we want it. Hello again and again major takeaway from this is writing will overwrite append will seek to the end and add to it and we can make reading files ridiculously simple. 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 void realms 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 you to me. 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.