 What's up guys, Root from NullShel.com here today bringing you back with some more Python tutorials. Check it out. Let's get idle fired up and we can stare programming and create a new window with control n. I'm going to save this as file.python. I actually saved it as phi.e.python. That's a damn shame. Let's save it as something else. Let's save it as file.python. There you go. All right, let's get our shebang line started up here. Environment Python. And I'm going to create an object oriented skeleton for us to work with. We don't need any inheritance because we're only going to be working with one object today. Get our constructor going on with the initiate keyword with two underscores on either end and then the self keyword as a parameter. I'm going to have it print something. Let us know that we're in business. Let's get some new lines in here so it's a little cleaner. We're going to create that object. It can be an instance of the base class and that's all set. Okay, cool. Now today what we're going to be talking about and learning about and all that good stuff is file handling and working with files. Now in this video we're only going to work with opening files and being able to create them and write to them and that sort of thing. But you know that's definitely a good start. So let's begin. When you're working with files usually you can use them as a sort of idea to save your data and save your information. You have a gateway to be able to save variables just in case your user ever stops the program and runs it again at a different time. You have external configurations. You can set up settings and just the way things you can almost create a save state for your program if you want to. And that's kind of convenient. You're able to write to it so you can save your data and you can read from it and get your data back. You would be able to understand variables and that sort of thing. So let's get started though. When you're working with files you need to create a file handle or a file object or just something that's going to be able to work with the file. So let's do that. Let's create a file handle object and we're going to use our Simon operator and use a new function called open. Now open is an interesting thing. This is the function that allows the gateway to open the file obviously. And let's check out our parameters here. We have open as the name and then we have the mode and buffering. But it looks like mode and buffering are optional so we don't really have to worry about those in too much detail yet. But let's give it a go. Let's try. Name is obviously going to be our file name or the path to the file. Now most of you I'm assuming are on Windows so you probably be typing in your C drive and then like documents and settings and then your username like John and then you got to put on your desktop because you have necessarily long file system and all that crap. But you're going to be wanting using these off forward slashes because if you use backslashes like you're used to seeing remember that the backslashes are mean they represent an escape character. So you'd have to use double backslashes so it interprets them as a real backslash. But I also kind of recommend you run Linux and make things make your life a lot easier. But hey whatever. On Linux obviously you can just type in your short little file name here and then I'm going to call mine a let's see file dot text because I'm the unoriginal whatever. So let's just plain old run this we're going to open the file boom we should be said right. No you're wrong. The thing is we don't have a file yet called file dot text. If I open up my terminal and we check out what's in here I can get rid of that stupid five dot e dot python file and then we don't have anything in here named text file dot text. I have a test dot text and I want to remove that because that's just dumb. But yeah we don't have this sort of file dot text. We have to fix this we have to be able to create that file from our program. So what we do is we work with that mode option. We can go in here and supply whether we want to read from the file whether we want to write to the file and there are a lot of other things you can do as well. You can you can do both at the same time if you need to and some other stuff. So we're going to go more in depth with it as the video series goes on and progresses obviously as you know. But let's get started with the right option first. You're going to need the first letter of whatever you're trying to do. And in my case we're just going to write to it. So if the file doesn't exist it's going to create it. If we run this program now we get our hello world no errors we are set. We are in business. So let's go back to our terminal. Let's see ls and now we have file dot text right here. So if we remove this remove the file again remove file dot text that's what it is and then we check it out as obviously it's not there but if we run our python code hello world we switch back to our terminal we look for output and boom there it is again because it creates the file even if it's not there. If it is there and we run the program it's just going to grab it and write from it that's that's all. So now let's begin to write some stuff in there. We're going to want our file handle and then because if we if we open up the file first of all let's I'll open it up in a text that are file dot text file dot text there's nothing in there it's a blank file. So let's go back into our program now and let's start writing some things. What we're going to be doing is we're using our file handle option I'm sorry our file handle object and then using the function that called write. Now we're going to want a string that we can write to it. Let's write um this is a new file that I created. Okay and now that we're done working with that file though we don't want to just leave it open that's kind of dangerous there could bring about some problems and some conflicts later on and because we're just those awesome pragmatic programmers we're going to want to go ahead and clear this up. So what we do is we we close the file and we just use that function close. When we're done working with it just don't leave it open because that could be problematic. So let's run this again though let's run this no no errors and let's check out what we got here. Let's uh let's nano file dot text and now you can see this is a new file that I created it's written what we wanted. So let's try it again let's run the program once more and let's uh actually let's let's set some variables. So let's make our program just a little bit better. String to write first of all can be our little thing here. Replace this with string to write paste that in there let's get our file name can be equal to what it is that we're opening because it's usually a good idea to just save everything as a variable especially if you're going to be using it more than once. So you can use file name okay cool so let's do let's get some out but though here let's print out file opened oh we'll quickly write out there file written to add on string variable of uh actually we don't even need to we don't have to cast it because it's already a string variable string to write and we should be set. Let's see what we got here file opened file written to this is a new file that I created let's actually add a tab there so it doesn't look so weird but if we go back to our program or at least up to our file it's going to say this is a new file that I created and we run the program again we run it and we go back to our terminal and there it is again this is a new file that I created but wait a second this is wait a second this is a little weird didn't you kind of expect it to just repeatedly say this is a new file that I created this is a new file that I created and this is a new file that I created because we're running it so many times. No, it's actually a little bit weird. Because we're writing to the file, we're taking everything that was already in that file, and then just erasing it and filling it in with what we write to it. So in our case, string to write. So let's try it. If we save the program, if we save our file.txt just like this, as file.txt, and we run our program, it's written to it, and now we can open it up again. And there it is. This is a new file that I created. It erased everything that was in there and just put in this. Now this could be kind of problematic. You might not want this. So let's go ahead and try something new. Let's er, let's remove the file. That'll make things easier. And then we can go into, go into our Python program. And we want to change the mode to A. Because this will let us append to the file or add new things to it. So let's change it. A. Awesome. Now we can file handle write. And then let's change the string write variable, string to write anyway, to a new line. Because we want to be able to add more and more and more to it. So let's give it a go. Run our program. File open. It's created because it wasn't existing to begin with. We wrote to it and we've added this new line character. So everything looks grand. Let's check it out in the program. Let's check it out in the file anyway. Nano file.txt. This is a new file that I created. Okay, our text is filled in and we have a new line character. We can go up and down here. So let's run the program again. And again. And how about let's change the string variable here to, because that's funny. Laugh. And run it again. And now let's check out our terminal. That's nano file.txt. And boom, check it all out. We've run the program like four or five times. It's put in exactly what we wanted it to. And it's appending it over and over and over again. So this is great. We're able to write to the file. We can add things to it. There are things that already exist, that sort of thing. But we're missing a little bit of functionality here. Don't we want to be able to read from the file too? Well, well, ladies and gentlemen, that is a cliffhanger. There it is right there. Smackdown on the ground. That is a cliffhanger. You're gonna have to watch the next tutorial to find out. See you again. Bye.