 Hi folks, I want to do a real quick refresher on file input and output syntax Back in your intro to programming or intro to computer science class You may have learned how to do file inputs in this style that I'm showing you here on the screen Where you've got a variable and then you assign to it the results of calling this Open function and open would take the name of the file on your hard drive as well as a mode This r thing here and then a sign this creates a file variable a type of file You assign it to this variable Then you do some stuff with the contents of the file and then finally you close the file you opened it You better close it right and there's there's good programmatic reasons for doing that I want to show you and maybe some of you have already seen this But I want to make sure that everyone is using this style of interacting with files It's a little bit more concise it Takes care of closing the file for you which is nice and also it's good for error handling and you know Maybe you haven't seen why that's so important yet, and that's okay But just know that this style over here that I'm about to go over with you is Best practice it's what you should use and that's what I'm going to expect you to use in this course So let me go over the details of it in code All the important kind of stuff I'm going to talk about here in just a second is on this slide which you can go through at your leisure So come back here review this make sure you understand this syntax in this way of operating with files Okay, so let me switch over to pie charm and show you an example I'm going to drag myself down to the bottom real quick before I get going okay, so How does this work usually when you're reading files at least in in our class you are going to be dealing with Text files so things where you can just open it up and look at it Well, let me show you this one, so I've opened names dot text and it's just a bunch of names one per line right These things are strings in Python land. I think I've got about a hundred of them in here So we're going to do a lot of reading of files in this class to get some interesting data that we're going to crunch and do stuff with And they're all going to be text files in our class. There can be other types of files. We're not too worried about them right now So how do you read this file? Well, I'm going to show you the new style of doing it Okay, so the new style is going to be We're going to use a keyword with so this is the keyword You start a with Open and this open function call It has two parameters. The first is the name of the file And my file is names dot text this guy right here. I'm in files dot pi right now Names dot text for simplicity's sake the files that you're reading from or writing to Keep them in the same directory as your Python script. They don't have to be there But helps keeps things simple and tidy. Okay The second parameter to this function is the mode, right? There are three modes that we care about we care about reading Which is our we're just going to read data from the file W which is for writing and a which is short for a pen Okay, I'll talk about writing in just a second, but we want to read from this file. Okay now This is a function that returns a file object. The next little keyword as Assigns that File to a variable that we can then manipulate with other Python code I'm going to name my variable input file and then I terminate the line with a colon. Okay, so Input file here is a variable. You can call it whatever you want. You can call it X you can call it name file doesn't matter. I'm just happening to call mine input file Okay, now when I go to the next line, you'll see that it's been indented for me Okay, the width is a block kind of like a for loop or a while loop or if statement There's stuff that goes inside the width, right? So what we are going to do is we're just going to read that file line by line Okay, how do you do that? You use a for loop. So for example for Line in Input file, let's do something. Let's just be very simple and print out the line that we read. Okay This is your basic for each loop for each line in the input file Line is a variable. You can call it whatever you want. You can call it name. It's up to you You can call it line whatever you want, right? It's a variable, but this variable is going to take on Row by row the string That it reads from each line Okay, so it is going to read each line of this file as a string that is terminated by the new line character right so Whoops name will take on as a string each line of the file Ending with a backslash n the new line character, right? And the new line character is what tells Python and all sorts of other programs go to the next line, right? So after you get the characters for the word Michael go down to the next line, right? Okay, so if I run this it's just going to regurgitate to me the contents of the file Now you may notice there's actually two new lines here It's it's going to the next line and then it goes to the next line again Well, why does it do that? Well, the value of name here is like for example Alicia backslash n that's the string that it is read in so it puts a new line at the end of that string But then also if you recall print The print function also automatically at the end of it does a backslash n So if you don't want that extra backslash in the thing to do is to strip this guy, right? If you watch the the string Video we talk about stripping strings now if we read this there we go we get what we expect, okay? Writing to files is exactly the same actually Let me go back for those of you who learned how to open and read files in the old style this line here is Exactly the same as doing input file gets open names dot text for reading This line and this line are the same the difference is that Once you exit out of this block like say once you get here This is gonna now be automatically close the file for you Great, you don't have to worry about it And also if you made some sort of error here in Python crashes out and all red stuff that file will still close for you That's a good thing. Okay. That's a good thing as Martha Stewart would say all right writing with the font to a file same thing Same sort of format I've already made a list here a list of strings. Let me excuse me Sorry, just gotta check time real fast. I've got a list of strings here. I want to write this list of strings to the file Let's do that Syntax format gonna look the same with open Let's write to states dot text That'll be the name of the file and our mode is gonna be W for right Now I need to define the name of the variable where this file is gonna live. I'll call it output Okay, that'll be the name of my variable Now before I go any further. I want to go over here and show you I actually already have states dot text And it's just got some random numbers in it Beware that when you write a file with a W here, you are going to destroy Anything that already exists in your file. It's gonna truncate it Empty it out fancy word for make it empty and then fill it back in. Okay. Well, that's right So what I would like to do is I want to write each one of these states Into the file. Well, this is a list. So let me loop over the list with a for loop for state in states I Want to write how do I write to a file very simply I take The file variable and I say output dot write and then the string I want to write Okay Outputting writing to a file always takes a string parameter if you try and write integers or floats or something It's gonna scream at you unless you convert it to a string first. Okay, so that should do it. Let's run it Okay, I'm still getting my output from reading it in the actual writing of the file itself does not produce any output If I comment out the reading of the file No output. Okay, let me go over to states Okay, first thing Maybe this isn't quite what I expected, right? It looks like I've got all my states here, but they're all crammed on one line Why is that? Think about it for a second The other thing you'll notice is the random numbers that I had in here before are gone the W mode got rid of them All okay, so this isn't quite right though What I really want is one per line here. So how do I realize that? Well, I Am writing strings here. What do I need to put at the end of a string in order to tell Programs. Hey, I want you to actually go to the next line. There's a special character. That special character is The new line. Okay, so these strings don't have new lines at the end of them I need to add them manually. Well, one way I can do that is through string Concatenation, right? So I take state I concatenate to the end this backslash in This gives me basically a new string. That's gonna look like, you know, say Alaska backslash in and now it's gonna write it out so let's rerun our code and I'm using the keyboard shortcut to rerun always you can right-click and Do it yourself If I go over to states now things look the way I want, okay? So whirlwind tour of this style of reading and writing from files. This is what I'm gonna expect you to use in the course Study it make sure you understand what's going on here. Okay. Take care