 What's up guys, Vruthanel here bringing you back with some more Python tutorials. We're looking back at our little extravaganza with files and we're being able to read and write from them and that sort of thing. So let's continue. I'm going to fire up idle if I can type anything today before I can speak even a little alone. And then let's get the terminal open too because we might want to be able to see what we're doing here. So now I've created a file, file.txt, the same one that we were working with earlier. I did sort of modify some of the lines in here. Now it says welcome to the file. This line right here is my favorite and how much code could a coder code if a coder could code code. And let me tell you right here now that is the most absurd tongue twister you will ever say in your life. I can get the whole woodchuck wood thing but coders coding could? I don't even know. Alright, let's open up idle. Let's create a new program here. I'm going to call this file.python. Just get our subang line, build our little skeleton of a program. We're going to be working with object-oriented programming just because that's a good idea. I don't know why you wouldn't want to work with object-oriented programming. It's definitely a good practice to let yourself get into. Some language actually really enforced it like Java. I don't know. Okay, let's print out here. Hello world. New line, new line. And now we can create a new object and that is an instance of our base class. So that works fine. Let's give it a go and that's awesome. Okay, so you guys remember what we were working at beforehand. Let's do what we can do here. Let's create a file name first of all. And we'll call this...actually we don't have to call it anything. It's already the name of the file. Home John and then file.txt. Actually, yeah, it is file.txt. Okay, so now let's get our file handle going. If you guys remember what that is in the last video, it's that object that's going to be able to let us read and write from the file. And we're going to open the file name with the read mode. So we can run this and that works perfectly fine. Just to be good practice too, we're going to want to close that. That works okay. Remember that this mode is an optional parameter. It's read by default. So we don't actually need that dash R. We can just open the file name the way it is. Boom, there we go. So let's...we got file here. Actually, file handle. File handle is not defined because we've closed it. So let's try working with this inside the interactive shell for now and seeing what we can do. I'm going to get that line though. I want to be able to... Okay, so now we have file handle and we have some things we can do with this or not. File handle is not defined yet because we need to get that whole line empty. Delete, delete, delete, delete. Okay, now I've got something to work with. Sorry for that little...sorry for that inconvenience, guys. We've got file handle and let's check out the things we can do with it by putting that dot selector and then choosing the control space to look at your auto completion feature. And let's check out what we can do. We can read...we can read line and we can read lines. Okay, so let's see what these all do. File, read. Let's just...let's try that one first off. File, read. We welcome to the file. This line right here is my favorite. How much code could a coder code if a coder code code code? Okay, we got ourselves a little variable here. Just a string. And we can just...we can just have a...we set a new variable now. Let's say contents is equal to file dot read. Cool. Let's check out contents. Wait a second. What is this? Contents is empty. This is because you can only read the file once. You go through a little bit of a...at least statically, you can only read it once. You have an imaginary cursor that goes through the file. It reads it and then once it gets to the end, there's nothing left, obviously. So you might have to just reset it. You can...I think file handle seek might work. I don't know if it'll...now if we read it again... We check out contents. It'll work. Okay, cool. Yeah, seek let us... Seek lets us get to any position of the file. A byte or character. So in our case, when we select zero, it's going right back to the beginning. So now contents is equal to file handle read. So that reads the entire file. We have the contents of the file. So that works for us. What if we did... What if we tried to get all the lines though as an individual thing? Let's create a list of all these files. We can say...sorry, all these lines. Lines is a new list. Let's leave it empty for now. And now for line in file handle. And now this is going to be one of these other things. We can read lines. Now read lines works a lot like that. Itter items or that sort of function that you saw in the dictionaries video when we released one of those. It's going to iterate through every single line that it finds. So file handle dot read lines. And then we can set ourselves a code block here and we can print out that line. And we can do lines. And we can add on a new instance of this line. So let's see it in action. Oh, that's right. We have to go back to the beginning. We got to use our file handle seek thing. Good thinking, guys. You are so smart. What would I do without you? File handle dot seek zero. Let's run it again. Welcome to the file. This line right here is my favorite. How much code could a coder code if a coder could code code? Okay, so look at... We have a little bit of white space here though. Remember we have these slash N characters, our escape characters to mean a new line. And remember that print creates a new line along with itself. So what we could do here is... First of all, let's check out lines and check out our list. And we have... Welcome to the file. This line right here is my favorite. How much code could a coder code if a coder code code code? So let's loop through this variable now. Let's look through for line in lines. We can just loop through everything that's inside that list. And we can print out that line. Get to the end of our code block, and there we go. Welcome to the file. This line right here is my favorite. How much code could a coder code if a coder code code code? And then the thing is, too, you have these multiple new backslash Ns. You're creating multiple new lines. So you can use that print line with a comma to not create a new line because line already has a line in itself. This works the way we expected it to. It has all the lines without the white space. It's reading exactly what's inside that file. So now that we have these in a variable and we can manipulate these things, we've sort of accomplished our goal here. We have successfully read from the file. We've looked at a couple functions here. We've looked at our read lines, which will go through them line by line. We've looked at read, which will get the entire thing as a string variable. And we've checked out seek, which will let us decide which position in the file anyway that we want to look at or start reading from. So this in conjunction with the file right options will allow us to create a more versatile program. We can save our information. We can read in and read out of a file, that sort of thing. So let's see what else we can do, though. Let's use our help function, which is a new function that you guys might not know about. But obviously it'll give you information. It'll help you with anything that you pass to it. So we can just pass in, let's say, open. Because that's what's going to let us use a new file. We don't need any parameters, any function, sorry, the parentheses because you're not passing in any parameters. And it is a function, but you wouldn't consider it here in this case. So let's try it, though, help open. This is what it's going to let us do. Open. We can open those names. We can have the mode, whether we're reading or writing from it. And this is exactly what happens. So we can check out file.doc for more information. So if we just type in help, and then we can pass our file handle to it, or we can just pass in a file, we can see everything that they can do. Let's check it out. These are all the functions. These are what they can do. It has a little bit of a description and that sort of thing. So you should be able to read up on this on any other thing. You can decide, you can use tell along with seek to get the current file position. You can remove things from the file. You can write to it, of course. You can have write lines along with read lines and read line and that sort of thing. So look at this, man. You have so much functionality here that you can use. You as a programmer have the most control here. And that's kind of the lasting impression that I want to give you guys today. You as the programmer have the control. You need to know how you can do it. Knowledge is power in the world of computer science. So thank you guys. Thank you for watching. Thank you for listening. It'd be cool if you could leave me a comment. Let me know what you think of the video. Maybe like the video. I don't know. I don't know. Just some ideas. Maybe subscribe. It's whatever you want to do. But thanks again, guys. I'll see you in another tutorial. Bye.