 Hey everybody this is Brian welcome to the 14th Python tutorial today We're going to be writing a file. So you can see I have this beautiful test directory all set up with nothing in there And we're gonna actually make a file. So we're gonna say import OS And we're going to just as path. Why not? And I need to actually set the path here And I'm just gonna make it a text file that way it'll open up in my text editor Now what we need to do is do a bit of magic here if you will so we're going to Make a function and we want to actually detect if that file already exists so if And we're doing this Because I don't want you to accidentally like overwrite some important critical file Like your vacation folder, you know your vacation pictures or your resume or your homework or whatever It would not be good if you did that because once it's gone. It's just gone It does not go to your your recycle bin or anything like that. It's just gone All right, so if it already exists then we're going to just return out and not do anything Otherwise what we're going to do here is we're going to say f equal open and We're going to give it the variable s file We're going to give it a w for write mode and we'll discuss modes here a little bit in depth And we're going to say try we're going to try writing to that file and we're just going to say Hello world Rn and we're going to say this is a new line That's something you'll get Throughout your programming career. You'll see the slash r slash n the slash is an escape character I think we've talked about that in the previous tutorial where if you're doing on a window system like C colon slash it'll actually error out because it's going to escape the string Now what it does is it actually takes this next? little digit behind the slash and turns that into a special character like slash r is a Return makes a new line I'm sorry. It's just a hard return and then n is a new line So whatever you do this on the keyboard you hit the enter key. That's actually two characters in memory I think it's like 10 and 13. I might have this flipped around but slash r slash n character turn line feed. It's actually pretty common and We've done air handling before so we just want to print out if there's an error that way if you have a boo boo you can try and figure out what's going on and Finally, this is a good example of why you have the finally block if F is not none None in Python's a special keyword that means it's not there. There's just nothing to it. It's never been assigned so That's a mouthful. So what we got here. Let's kind of go through this is we have our function As a variable that we're going to pass to it, which is going to be the file name If the file exists, it's going to print out a message in return Otherwise, it's going to open the file which will then create a blank file on the hard drive Then we'll try to write to it If there's an error we'll print out the error and then finally we will close the file One thing you should get in the habit of is called flushing and it does Sadly exactly what it sounds. It's just like flushing a toilet. It flushes everything from memory down into the hard drive When you're creating a file Literally what you're doing is you're creating data in memory And then taking that data and shoving it down into the actual hard drive the actual hardware Um, so flush literally flushes from the memory to the hard drive So I wish they would have picked a better word for that, but it is what it is So we're going to just write file as path And let's run this bad boy and see what happens And it says process finished exit code zero. I probably should have put like a little hey We wrote the file in there, but you can see there's our little test file And if I bring it out here, you can see hello world. This is a new line. So that's our file Now if I attempt to run this again File already exists So it's going to whoops. I just bumped the mic. Sorry about that It's going to get here and then it's going to return out and that'll keep you from accidentally overwriting a file Now Notice how if we just comment this out and run this again We're doing no No intrinsic checking to see if that file exists. We're just going to overwrite it Whoops, you can see in my little notepad editor. It's saying Whoa, this file has been changed. What's going on? And let me see if I can actually drag it over here. Yeah, there we go The file on disk has been changed. So Not all Text editors are smart enough to do this, but fortunately the one in Ubuntu is So we're just going to Cancel that So it detected there was a change now when we go out to the actual file and reload this You see it's the exact same thing That's write mode right will completely delete the file and start over If you wanted to append the contents you'd use an a for append And now if we run this let's say a few times I'll click it three times. Why not? Let's run that You'll see that we have Three distinct things now we didn't put the hard return in there So it's just going to start right where it left off if we would have put the slash r slash and it would have actually, you know broken it out like this So that's the difference between append and write That's all for this tutorial. I hope you found this educational and entertaining. Thank you for watching. Um Feel free to visit my website of void realms.com whoopsy And that way you can actually get the source code for this and other tutorials And be sure to visit the facebook void realms group. There's over 200 programmers in there that can also help