 Welcome back everyone today. We're going to be talking about how to read and write files in Python So it's a really common to want to save data across the times that you're running your script And we do that by writing data to the hard drive So to write data the hard drive we normally open up either a file or some type of database and today We're going to be talking about the how to deal with files. So Opening files is relatively simple We need to create something called a file handle first So the file handle can be named whatever you want. I'm just going to call the file handle FH and then just like setting up a normal variable We have our variable name and my variable name here is FH equals and then we want to Open up a file Okay, so I'm going to type open and it's a function that's built into Python You can see that it turned blue which means it was detected as a Python function and then I need to give this function a Name of the file that I want to create. So I'm just going to call it test dot text and then An attribute about describing how I want to open the file So what do I want to do with the file once I open it and you basically have three options you can either You can either Read so r equals read w equals write for a equals append Also for a type of writing. There's also What was it r plus R plus which is kind of read and write Okay, but what we're going to talk about today are the first three because they should pretty much get you As far as you want to go So the first thing I want to do is actually create this file test dot text Which means I need to open the file handle as a Write so I need to write to the file Down below in my terminal if I look in the directory Right now. I just have a bunch of Python files. I don't have anything called test dot text So we're going to create this file and then write something to it So since I'm writing I need to use the w attribute or argument to specify that I want to write data to the file so I'm just going to save that and then Just so you can see what a file handle Looks like without writing anything to the file yet. I'm just going to open up the file Just opening up the file and then I'm going to print the contents of FH and that will print Basically a description of the file handle. So let's go ahead and save that and then run it okay, so I'm going to run my Python script using Python and We see this IO Which is input output and then text IO wrapper and we have an argument named test dot text The mode is w for write and the encoding is utf 8 Which is my default encoding. Okay, so now if I look in the directory You can see that I have test dot text created If I try to look inside that You can see nothing printed out which means test dot text is empty. So that's right We opened up the file in write mode So that created the file on the disk in the same directory as the script that I'm running it in you can specify where you Want to create it? I created it in the same directory So now we have our empty text file and we didn't write anything to the file. We just printed the file handle. So That means that the text file is empty. Okay, and that's pretty much all there is to Creating a file, but we want to do more than just create a file normally. We want to write something to the file Okay, so as you can imagine we already have this file handle FH and we're going to be using the file handle basically every time we want to read or write Something in this case write because that's how we opened it So I need to do FH and if I want to write something I do dot write so FH period write and Then the text that you want to write to the file. So I'm going to say This is a first line And I want to write three lines to this file. So I'm going to go ahead and do Just copy and paste that I'll move this out of the way Okay, so now we've opened up the test dot text file in write mode and then I'm going to write Three lines to the file And then that's it and what I need to do once I'm done doing that is It's good practice to close the file now the script will close Once the script is finished running and we get to the end of the script then the script itself will exit and then eventually the files There are the operating system will close the file handle, but Once you're done with the file handle. It's good practice to just close it as soon as you're done Okay, so let's go ahead and run this You can see then nothing happened. We have our test dot text file And then if I cat or if I show the contents of test dot text We now have one Single line that says this is the first line. This is the second line. This is the third line So what's going on here? Why is it only? Showing up in one line and the reason for that is new line is a character It's a character that basically whenever you render something in a text viewer The new line character isn't shown, but it tells the computer to Separate these two things and then move this this to an the next line. So that new line character is Slash in so I need to Include slash in if I want this to be on one Line by itself and then the next line be on a different line So let's do two slash ends Let's run the script again And then see what the results are So now I put slash in which is a new line character So this is the first line and then there's actually an invisible character at the end here And that's the new line character that says okay now we're here go to the next line So then we have the next line We have a new line character and we have the third line and I did not add a new line character to the third line So you can see that this command prompt is on the same line because it doesn't is it isn't forced to the next line So just a reminder You do have Kind of these control characters these invisible characters In text files that you will need to add yourself if you want them to Show up like you expect, okay Right, okay, so now we have Opened a file test dot text for writing and then we've written three lines and then Let's run it again cat and then we have each line on its own individual line because we added this new line control character Once we wrote everything to the file This external test dot text file I'll just open it up in Notepad to show you so just like you would expect in notepad we have three lines on three separate lines Okay, so it's a normal text file separate from your Python script Okay, so this is a different file from your Python script So you can save whatever you want inside that file. There's basically two types of files we are dealing with a Text file and then Python also supports binary files and binary files are a little bit more advanced So I'll leave that for later Text files while you're practicing should get you Pretty far. Okay. Okay. So now we have our file that's been opened And we can write to it. Well writing is fine But eventually you're going to want to read something from the file So as you would expect I'm going to change the file handle name to I don't know read file handle. Remember, this is a Variable name so you can name this whatever you want. Okay, so read file handle Equals that I'm going to use open again So open here We want to open test dot text for reading. Okay, so I need to open test text and Then instead of the w for right, I need to use our for read. Okay And then now we have this RFH. This is the read file handle. I Could have called it even FH again and no problem and to read something We can just do RFH dot and can you guess what it is? Just like right we're doing read and read is its own function. So RFH Dot read. So this is the file handle We've set up open for reading with the R and then once we have the file handle created Then we can do dot read and then it will read the file So now let's go ahead and run that and you notice before whenever I ran the script It did not print anything on the screen. It just wrote Text to this text file now. I'm reading from the text file And printing everything on the screen as I read it using the read command now read. We'll just read everything But we can specify exactly what we want to read Now that I already have this test text test dot text created I'm going to go ahead and move the right data just to clear up this screen a little bit Okay, so read reads everything in the file and sometimes we want to read everything in the file and Put that into a variable or start to do something with it But we might want to read for example just individual Characters so let's say we know we want to read only 10 characters from This file. Okay, if we know for example what text we want to read We know it's exactly five characters long and we could for example read the first ten characters Okay, and then this is a if I did five Then I just get this and a space if I did seven Okay, now Imagine that I am reading seven And whenever we're reading Every time you read It moves a Pointer along the file handle so once we read these these first seven characters The pointer doesn't go back to the beginning it actually kind of stays so let's say we read These first characters now the pointer would be at in and then if we read again It would start at this bracket. Okay, so what we'd get here Let's just run this and show you what happens so this is I Read the first seven characters and then I read read the next three characters and then And then read the next ten characters Okay, this is and then That was the first seven characters the next three characters and then the next ten characters So as you read Whatever you read the pointer is going to stay at the last point that you read It could be a line or it could be individual characters and then the next time that you call read The pointer is going to go from the next location. Okay, so as long as you have this file handle open it will remember where you what you've read so far and then Kind of hold your place at that point for the next time that you read So just be aware of that it doesn't reset every time you do a read It doesn't go back to the beginning. It'll just stay where you left off. Okay That's true for just read which is giving me characters here. This is also true for Let's say I don't know how long a line is going to be in my case. I don't know how long My Individual lines are in the file So let's do cat text us. So for example, these lines are different Lengths, so let's say that I wanted this entire line, but I don't know how long this line is Well, instead of read and that would read everything or at least the characters that I Specify I can use read line Okay, which is really common to do if you have a text file. You want to read the line instead of reading everything Okay, so let's read files and then I read line and I get this is a first line Okay, now you might be thinking. Well, can I put something? Can I put a number in here and then read line number 10 Doesn't quite work that way. So this is still reading the first line But it's gonna be the first 10 characters from that line. Okay now Yeah, okay So just like before let's say I read line I can get the first line and then if I read the line again What do you think will happen? We'll go back to the first line or will we go to the second line? This is the first line. This is the second line. Okay, and it's doing two reads And basically it's adding this new line in between Okay Now if I wanted to read all lines Then I can do read lines In plural so I just add an S to that and it will read multiple lines for me Okay, now What is the difference between? Just read which gave me all of the lines and read lines Well, whenever I do read lines, this is returning not individual lines But what is this the square bracket should tell you what it is. It is an array, right? This is a list of lines and you can even see that the Control characters are also shown so by reading lines I get an array of all of the lines in the file Okay, if I just did read It will print all of the lines in the file Okay, but it will render the control characters And yeah, I don't get an array. I just get the lines directly. Okay, so read lines We'll give me back an array. Now, you might be thinking why the heck would I want an array? Read lines Why would I want an array? Well, if you have the array of all of the aligned Lines first off, you can see what control characters they're using very easily Second you can loop over the arrays like we've talked about so before we were talking about loops and We were looping over An array and this would be an array that we could also loop over and do something with each line or check each line for For example, the keyword second and then do something if it's the second keyword Okay, so having something in an array format is very easy to work with if you want an array with all the control characters Use read lines if you just want all of the data from the file Just use read and then if you only want one line at a time use read line and then remember That's every time you read It's going to Hold your pointer for you So I'm gonna read one line and then next time I call read line It'll print the second line the next time it'll print the third line Okay So Just as an example Let's go ahead and run through a file using a for loop. So it's pretty common. Especially if you have a text file Let's say that you have data that you want to access on each line and then you want to Understand what's on that line and do something with it using an if statement. You want to check something about the text Okay, so what we can do is open the file test dot text Read all right with the R and then I can use a for loop. So a for loop is four and then instead of doing Yeah, instead of doing it the old way that we showed we can do for line in RFH okay, now what this does is it takes It does basically read line it takes one line from the vial handle and then assigns that line to A variable here. I'm calling line. I could have called this anything. It can be L. It can be test It can be hello, like whatever I want to call this, but I usually call it what it is So I'm taking one line from RFH which is the text dot test dot text handle and then I'm putting that line into the variable named line Okay, so for line in RFH and then I can just do print line now. What should happen here? Well, we're getting the line and then we're just printing it So we should get something very similar to what's output Whenever you do all of the read lines And it is exactly the same so you might be thinking well, why would I use this instead of read lines? Well, read lines is just returning everything At the same at the same time So you have one read read lines is returning an array of everything if you just do read You're returning everything at the same time You can't really do anything with the individual lines if you do read line You have to call read line over and over and over again manually This is kind of an automated way to call read line over and over again. What you can get from that is Let's say one two three four five six seven eight Let's say that I wanted to count the The length of each line so I want to know how long is each line In this file, I don't know why I would want to know that but let's say I wanted to know it Because I have the individual line now. I can do something like length of line So let's go ahead and run that and see what happens So we have our line and then it counts 21 we have our second line it counts 22 We have our third line and it counts 21. Okay. Well now that we know the the length of each of the lines Let's do an if statement. So if line if length of the line is Equal to 21 print Okay, so what should happen here we're going through We're reading an individual line from the file handle. So our read file handle is test dot text We're taking the line from RFH Putting it into the variable called line and again I can name this whatever I want and then I'm testing line using an if statement. So if the length of Line the number of characters in line is equal. Oops. I need to do a double equal sign Is equal because I'm testing to 21 Then print cool guy Else print. Oh, no Okay, so can you guess what would happen here? We have one line that's 21 one that's 22 one That's 21. So what we should get out whenever I run this script is cool guy should print And then oh no should print and then cool guy should print. Okay, so let's go ahead and Run that and that's exactly what we get Okay, so the whole point of using a for loop over these lines instead of reading Everything at one time Which you might want to do if you're just printing something But if you actually want to test each line Then it's better to use a for loop over each line Of course, it depends on the data you have and what your goal is But very often we use loops and then loop over each line and then decide to do something With that particular line. Okay, so that's a little bit about Opening files read and write writing to a file closing the file Keep opening I need also do a close here. So RFH equal Don't forget your closes like I just forgot my clothes. Okay Right, so we want to close our files whenever we're done with it and Then We can read we can use read line We can use read lines and we can also loop over individual lines To get some data out of it. So we're able to save data to the hard drive In test.tex that's separate file from your script. We're also able to read back out of it now overall What's this for? Well, if you're running your script Whenever the script exits everything you were doing is essentially lost the script doesn't remember things every time you run it So you need to save anything you want to remember into a file Into a database and that's what this is for so now whenever you're running your script You can ask the user for their name and then you can save that name into a file The next time you run your script you can load that name from the file and then use it While they're running the script and you don't have to ask them for it again for example, okay? So that's it for writing files and reading files from the disk. Thank you very much