 Hello everybody, today we're gonna be talking about creating, reading and appending files in Python. Now when I say creating a file, I actually mean creating a file in File Explorer. Now let's go right down here to the bottom of our screen and open up our File Explorer. This is the folder that we're gonna be using in this video. Now this is not our default file path and I'll explain that more in a little bit. Our actual default file path is gonna be right here with this homepage. So if we're just creating a file and we're not specifying the file path, it is automatically gonna go right here. But I don't wanna rely on the defaults. I wanna be specific and write out my entire file path and we'll do that in just a second. So what we're gonna do is go ahead and type open. Then we're gonna do an open parentheses. The open function only has two arguments. It has the file path and it has the mode. So we're gonna do an apostrophe. Now we need to get our file path and if we had just done something like fakefile.txt and there's no file path before it, it's automatically gonna go to the default file path which was our homepage right up here. We're gonna be more specific and specify our file path. So let's go down to our file explorer and open it up. I'm gonna click in this bar right here. We're gonna hit control A to make sure that we have everything selected and then a control C to copy that and go right here before this fake file and hit control V to paste it as well as a doc slash to make sure that we're separating the folder from the actual file. The next thing that we need to do is add the mode. Now the mode is gonna specify how this file is opened. Within the mode argument, you can specify if you are creating a file, writing to a file, appending or reading a file. So for what we're doing, we could put an X which is gonna say we are just creating a file and that's all it will do and that would be perfectly fine to do but we also want to write to this file. So if we put a W, it's going to create the file as well as allow us to write to it. So we're gonna keep it as a W and then we're gonna assign this to a variable and we're just gonna say write underscore file equals and then this is our entire statement to write to this file. Now really quickly, I'm gonna run this and it's going to fail and this is probably something that a lot of you guys have experienced if you've ever tried to do something like this. Let's go ahead and click shift enter and here is our error right here and I have seen this error a thousand times. All you have to do in order to get rid of this error and fix it is put in our right here before your string. Now what the R does is it's gonna read in this string as a raw text. Instead of interpreting these backslashes and colons and periods, it just reads it as a raw text as your file path. So let's go ahead and let's run this again. We're gonna hit shift enter and now we have opened that file. So let's go right down here to our file explorer. Let's hit refresh and there is our file and it is open. As you can see right here under the status we have this kind of refresh symbol. This means that this file is currently being used and it's open. So now what we can do is minimize out of this and we're gonna write to this file a string that'll actually go into the file. So we're gonna copy this right file. I'm gonna say write file dot write. So all this does is actually open the file in a writing mode and the dot write actually allows us to write to the file. So we're gonna do an open parentheses. We'll do a apostrophe and now we can write our string that we wanna add. So we can say this is our first sentence in our file. And now what we can do is run this and the 39 means that we are inserting 39 characters into that file. So let's go right down here to our file explorer and open this file up. And as you can see, our text was actually inserted into this file. I'm actually gonna bring this over here just so that it's easier to see throughout the video. So let's exit out of it. Let's minimize this. Now something that's really important is actually closing out a file when you open it. So right up here, we have it open in a write mode. Now in just a little bit, we're gonna wanna append information to this file, but if we already have it open in the write mode, that may cause issues. So all we're gonna do is do write file dot close in an open parentheses and then we're gonna run this and it will actually close down the file. Well, let's hit shift enter. Now let's take a look at that file in file explorer. And as you can see, the status is a check mark, which means it's no longer open. Let's get out of this. And now let's append more data to this file. Let's go right up here. Let's copy this entire thing. We're gonna say A or append. And then we'll go append underscore file. So what this is gonna do is now we're gonna open this file again, except now we're doing it in an append mode, which means this is already an existing file that we are now going to write to. Let's copy this right here. We'll put it right underneath it. We'll say append dot file because now when we open it, we're assigning it to this append file. So we'll do append file dot write and we'll say this is our second. We'll do a space because I want a space in between the sentences. And then we'll add this close right here and we'll do append file. So all in one cell, we're gonna open the file right to this file and then we'll close the file. So let's go ahead and run this. And now let's go look at our file. So as you can see, it was opened and let's just refresh it to make sure. And as you can see, we have added the second sentence with our space in between it. Now let's get out of there. So up till now, everything that I've shown you is technically correct, but it isn't actually best practice. I'm now gonna show you how to use something called a context manager. A context manager basically allows you to do everything that I did in this cell in only two lines and automatically close out that file for you. So now we're gonna actually append a little bit more information to this file before we start reading it into Python and not having to open up the file every time. So let's copy this. And with a context manager, the keyword with specifies that we are creating this context manager. So with this open file, we'll say as, and we'll use append file again, as append file, then we're gonna do a colon, we'll hit enter and it should auto indent for us. And then we're gonna say append file dot write. And then we'll do a backslash n. This is our third sentence in our file on a new line. And this backslash n is just gonna basically hit enter in our file and create a new line and then append this data to that new line. Let's go ahead and run this. And after we're done, we won't have to say append file dot close like we did up here. It's gonna automatically do that for us. And as you can see, it's just a little bit cleaner. It hasn't indented. And so you can more easily read this, especially if you have a lot of code, doing something like a context manager is gonna just make things a lot easier. So let's go ahead and run this. And now let's go back and open up our file to refresh it, open it up. And there is our new sentence right here. I actually added in a space, but there is our new line right there. Let's exit out. Let's get rid of this. And now what we're gonna do is actually read in that file instead of having to open up that file manually, I'm just gonna read it in right here. Let's go ahead and copy all of this context manager, paste it right down here. We're gonna say read. And we'll call this read underscore file. We'll copy this down here. Now we're not writing to it. Now we're just reading it. So we're gonna say read and an open parentheses. So now everything that we just saw in that file we should now see here in Python once we read it in. So let's hit shift enter. And as you can see, nothing happened. It's because we didn't actually print the information off. It read it in and it did read the information, but we didn't get any output because we didn't print anything. All we have to add is a print statement right here with a parentheses around this. Now when we run this, we should get our output. Now let's copy this append file one more time because what we can do is we don't actually just have to put in string right here. We can actually create a variable and put the variable within that right parentheses. We're gonna say multi underscore line is equal to and we'll do a triple statement. And I'm gonna say this is the fourth sentence. Let me copy this. We'll say fifth and sixth. It's just capitalized to keep it consistent. Now we can copy this multi-line variable and we're gonna put it right in here. And now we can write to this as well instead of just writing an actual string in there. So let's do shift enter. And now let's read in that file again. So as you can see, it worked properly and that really is the nuts and bolts, the meat and potatoes of creating, reading and appending files in Python. I hope that this was helpful. I hope that you learned something. If you did, be sure to like and subscribe below and I will see you in the next video.